ViewPager初步用法(二)

MainAvtivity.class文件:

package com.example.guocong.viewpager;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import static java.lang.Boolean.FALSE;

public  class MainActivity extends AppCompatActivity {
    private ListviewList;
    private ViewPager pager;
    private PagerTabStrip tab;
    private ListtitleList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /*
        通过View对象作为ViewPager的数据源
         */
        viewList=new ArrayList();

        View view1=View.inflate(this,R.layout.view1,null);
        View view2=View.inflate(this,R.layout.view2,null);
        View view3=View.inflate(this,R.layout.view3,null);

        viewList.add(view1);
        viewList.add(view2);
        viewList.add(view3);

        //为ViewPager页卡设置标题
        titleList=new ArrayList();
        titleList.add("第一页");
        titleList.add("第二页");
        titleList.add("第三页");

        //为PagerTabStrip设置样式
        tab= (PagerTabStrip) findViewById(R.id.tab);
        tab.setBackgroundColor(Color.BLACK);
        tab.setTextColor(Color.WHITE);
        tab.setDrawFullUnderline(FALSE);
        tab.setTabIndicatorColor(Color.GREEN);

        //初始化viewPager
        pager=(ViewPager)findViewById(R.id.pager);
        //创建适配器
        MyPagerAdapter adapter=new MyPagerAdapter(viewList,titleList);

        //ViewPager加载适配器
        pager.setAdapter(adapter);

    }
}

activity_main文件:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   >

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">
        <android.support.v4.view.PagerTabStrip
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/tab"
            />

    android.support.v4.view.ViewPager>
LinearLayout>

MyPagerAdapter文件:

package com.example.guocong.viewpager;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;
public class MyPagerAdapter extends PagerAdapter {
    private ListviewList;
    private ListtitleList;

    public MyPagerAdapter(ListViewList,ListtitleList)
    {
        this.viewList=ViewList;
        this.titleList=titleList;
    }
    //返回页卡的数量
    @Override
    public int getCount() {
        return viewList.size();
    }
    //View是否来自于对象
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }
    //实例化一个页卡
    @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 CharSequence getPageTitle(int position) {
        return titleList.get(position);
    }
}

另外,还有三个视图事件,根据需要写相关代码。

如果用Fragment作为数据源:

private ListfragList;

/*
        通过Fragment作为ViewPager的数据源
         */
        fragList = new ArrayList();
        fragList.add(new Fragment1());
        fragList.add(new Fragment2());
        fragList.add(new Fragment3());
        ---
MyFragmentPagerAdapter adapter1=new MyFragmentPagerAdapter(getSupportFragmentManager(),fragList,titleList) {
        };
        pager.setAdapter(adapter1);
        pager.setOnPageChangeListener(this);
    }
    ---

MyFragmentPagerAdapter.class:

package com.example.guocong.viewpager;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

/**
 * Created by guo on 2017/1/12.
 */

public abstract class MyFragmentPagerAdapter extends FragmentPagerAdapter{

    private Listfraglist;
    private Listtitlelist;
    public MyFragmentPagerAdapter(FragmentManager fm,Listfraglist,Listtitlelist) {
        super(fm);
        this.fraglist=fraglist;
        this.titlelist=titlelist;
    }

    @Override
    public int getCount() {
        return fraglist.size();
    }
    //获取标题
    @Override
    public CharSequence getPageTitle(int position) {
        return titlelist.get(position);
    }

    @Override
    public Fragment getItem(int position) {
        return fraglist.get(position);
    }
}

你可能感兴趣的:(android)