1. 程式人生 > >Android之ViewPager入門使用

Android之ViewPager入門使用

1.layout佈局


activity_mian

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.lenovo.redball.MainActivity">
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/viewPager"/>
</LinearLayout>

tab1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@mipmap/tab1"/>
</LinearLayout>

tab2.xml等類似

其中:

定義一個View型別的List陣列集合,用於儲存N個xml佈局檔案, 

使用getLayoutInflater()方法載入佈局檔案,將N個xml檔案賦值View型別變數,或者直接viewList.add()加入到集合裡面。 
然後定義viewPager的介面卡,viewPager1.setAdapter(); 
引數為PagerAdapter類。 
提供的引數可以通過匿名內部類的方式實現,也可以繼承重寫的方式實現。 
重寫的方法有

public int getCount();
public boolean isViewFromObject(View view, Object object)
public
Object instantiateItem(ViewGroup container, int position); public void destroyItem(ViewGroup container, int position, Object object);

getCount()方法用於返回viewPgaer的數量,返回viewList集合數量就可以了。 
isViewFromObject()方法暫時不理解,暫時返回 view==object即可 
instantiateItem()方法用於滑到另外一個頁面時呼叫,返回viewList中當前頁的內容。 
destroyItem()從ViewPager中移除頁面。

MainActivity.java

private ViewPager viewPager;
    private LayoutInflater layoutInflater;
    private View view1,view2,view3,view4;
    private List<View> viewList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        viewPager=(ViewPager)findViewById(R.id.viewPager);
        layoutInflater=getLayoutInflater();
        view1=layoutInflater.inflate(R.layout.tab1,null);
        view2=layoutInflater.inflate(R.layout.tab2,null);
        view3=layoutInflater.inflate(R.layout.tab3,null);
        view4=layoutInflater.inflate(R.layout.tab4,null);
        viewList=new ArrayList<View>();
        viewList.add(view1);viewList.add(view2);viewList.add(view3);viewList.add(view4);
        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return viewList.size();
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(viewList.get(position));
                return viewList.get(position);
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(viewList.get(position));
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view==object;
            }

        });
    }