ViewPager-实现左右两个屏幕的切换

jopen 11年前

     什么是ViewPager呢?
它是实现左右两个屏幕平滑地切换的一个类,它是Google提供的。
具体使用方法在<SDK_DOC_PATH>/sdk/compatibility-library.html(SDK_DOC_PATH是自带Android文档的位置,需要更新到最新的文档才有)。
    ViewPager提供了一个适配器,方便我们对各个View进行控制,下面介绍如何使用:
1、下载来Android SDK & AVD manager的更新:Available Packages -> Android Repository -> Android Compatibility Package, revision 3. 它将安装\extras\android\compatibility\v4文件下
2、建立一个Android项目,在上述安装目录下把 android-support-v4.jar添加进项目,如果是最新的ADT,只需要右击project->Android Tools->Add Compatibility Library
3、在Activity中使用ViewPager

4、如果ViewPager是用xml去定义的,需要写上全局的路径,如:

    <android.support.v4.view.ViewPager        android:layout_width="match_parent"         android:layout_height="match_parent"         android:id="@+id/awesomepager"/>  
5、建立一个PagerAdapter,实现如下方法:
getCount(), instantiateItem(), destroyItem(), isViewFromObject() 。
    public class AwesomePagerActivity extends Activity {                        private ViewPager awesomePager;                    private Context cxt;            private AwesomePagerAdapter awesomeAdapter;                        private LayoutInflater mInflater;            private List<View> mListViews;                        /** Called when the activity is first created. */            @Override            public void onCreate(Bundle savedInstanceState) {                super.onCreate(savedInstanceState);                setContentView(R.layout.main);                cxt = this;                                awesomeAdapter = new AwesomePagerAdapter();                awesomePager = (ViewPager) findViewById(R.id.awesomepager);                awesomePager.setAdapter(awesomeAdapter);                                mListViews = new ArrayList<View>();                mInflater = getLayoutInflater();                mListViews.add(mInflater.inflate(R.layout.layout1, null));                mListViews.add(mInflater.inflate(R.layout.layout2, null));                mListViews.add(mInflater.inflate(R.layout.layout3, null));                            }                        private class AwesomePagerAdapter extends PagerAdapter{                                        @Override                public int getCount() {                    return mListViews.size();                }                        /**                * 从指定的position创建page                *                * @param container ViewPager容器                * @param position The page position to be instantiated.                * @return 返回指定position的page,这里不需要是一个view,也可以是其他的视图容器.                */                @Override                public Object instantiateItem(View collection, int position) {                                                ((ViewPager) collection).addView(mListViews.get(position),0);                                        return mListViews.get(position);                }                        /**                * <span style="font-family:'Droid Sans';">从指定的position销毁page</span>                *                 *                 *<span style="font-family:'Droid Sans';">参数同上</span>                */                @Override                public void destroyItem(View collection, int position, Object view) {                    ((ViewPager) collection).removeView(mListViews.get(position));                }                                                        @Override                public boolean isViewFromObject(View view, Object object) {                    return view==(object);                }                        @Override                public void finishUpdate(View arg0) {}                                        @Override                public void restoreState(Parcelable arg0, ClassLoader arg1) {}                        @Override                public Parcelable saveState() {                    return null;                }                        @Override                public void startUpdate(View arg0) {}                            }                                }  
就这样,我们可以很容易就实现左右两个屏幕之间的切换。具体页面到内容就靠大家去发挥了!