Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 詳解Android App中創建ViewPager組件的方法

詳解Android App中創建ViewPager組件的方法

編輯:關於Android編程

現在很多app一打開就是一個ViewPager,然後可以用手指滑,每滑一次就換一張圖,底下還會有圈圈表示說現在滑到第幾章~
通常這些圖片都是放功能簡介或是使用教學之類的,我的需求很簡單,就是上面提到的那樣而已。
有兩種做法,一種是找現有套件,查了一堆資料每個都跟我推薦ViewPagerIndicator這套,我之前也看過這套,只是看起來需要有fragment再加上google play范例好像載不到了,所以只好自己實做一個。
Viewpager的實作可參考Android ViewPager使用詳解裡面的程序碼:

@Overrideprotected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_intro);

  mViewPager = (ViewPager) findViewById(R.id.viewpager);

  final LayoutInflater mInflater = getLayoutInflater().from(this);

  View v1 = mInflater.inflate(R.layout.intro_layout_1, null);
  View v2 = mInflater.inflate(R.layout.intro_layout_2, null);
  View v3 = mInflater.inflate(R.layout.intro_layout_3, null);
  View v4 = mInflater.inflate(R.layout.intro_layout_4, null);

  viewList = new ArrayList<View>();
  viewList.add(v1);
  viewList.add(v2);
  viewList.add(v3);
  viewList.add(v4);

  mViewPager.setAdapter(new MyViewPagerAdapter(viewList));
  mViewPager.setCurrentItem(0);}
MyViewPagerAdapter
public class MyViewPagerAdapter extends PagerAdapter {
  private List<View> mListViews;

  public MyViewPagerAdapter(List<View> mListViews) {
    this.mListViews = mListViews;
  }

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


  @Override
  public Object instantiateItem(ViewGroup container, int position) {
    View view = mListViews.get(position);
    container.addView(view);
    return view;
  }

  @Override
  public int getCount() {
    return mListViews.size();
  }

  @Override
  public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0==arg1;
  }}

這樣子你就有一個ViewPager了


下面我們來具體看一下ViewPager的用法:
一、ViewPager創建步驟
① 在XML布局中加入android.support.v4.view.ViewPager

② 加載顯示的頁卡將Layout布局轉換為View對象

  (1) 

LayoutInflater lf getLayoutInflater().from(this);
  lf.inflate(resource,root);

  (2)

View.inflate(context,resource,root);

③ 配置Adapter(三種Adapter)

 (1)PagerAdapter 數據源:List<View>
 (2)FragmentPagerAdapter 數據源:List<Fragment>
 (3)FragmentStatePagerAdapter 數據源:List<Fragment>
二、代碼示例
新建四個Fragment和他們的布局下面是MainActivity的xml布局:

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

    <android.support.v4.view.PagerTabStrip
      android:id="@+id/tab"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="top" >
    </android.support.v4.view.PagerTabStrip>
    <!-- 底部顯示標題與上面的頂部顯示不能同時出現
    <android.support.v4.view.PagerTitleStrip
      android:id="@+id/title"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="bottom" >
    </android.support.v4.view.PagerTitleStrip>
    -->
  </android.support.v4.view.ViewPager>

第一種PagerAdapter

public class MyPagerAdapter extends PagerAdapter {

  private List<View> viewList;
  private List<String> titleList;

  public MyPagerAdapter(List<View> viewList,List<String> titleList){
    this.viewList = viewList;
    this.titleList = titleList;
  }

  /**
   * 返回頁卡的數量
   */
  @Override
  public int getCount() {

    return viewList.size();
  }

   /**
   * view是否來自對象
   */
  @Override
  public boolean isViewFromObject(View arg0, Object arg1) {

    return arg0==arg1;
  }
   /**
   * 實例化一個頁卡
   */
  @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));

  }

  /**
   * 設置ViewPager的標題
   */
  @Override
  public CharSequence getPageTitle(int position) {

    return titleList.get(position);
  }

}

第二種FragmentPagerAdapter

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
  private List<Fragment> fragList;
  private List<String> titleList;
  public MyFragmentPagerAdapter(FragmentManager fm,List<Fragment> fragList,List<String> titleList) {
    super(fm);
    this.fragList = fragList;
    this.titleList = titleList;
  }


  @Override
  public Fragment getItem(int arg0) {

    return fragList.get(arg0);
  }

  @Override
  public CharSequence getPageTitle(int position) {

    return titleList.get(position);
  }

  @Override
  public int getCount() {

    return fragList.size();
  }

}

第三種FragmentStatePagerAdapter(該適配器可以動態銷毀Fragment)

public class MyFragmentPagerAdapter2 extends FragmentStatePagerAdapter {//該適配器可以動態銷毀
  private List<Fragment> fragList;
  private List<String> titleList;
  public MyFragmentPagerAdapter2(FragmentManager fm,List<Fragment> fragList,List<String> titleList) {
    super(fm);
    this.fragList = fragList;
    this.titleList = titleList;
  }


  @Override
  public Fragment getItem(int arg0) {

    return fragList.get(arg0);
  }

  @Override
  public CharSequence getPageTitle(int position) {

    return titleList.get(position);
  }

  @Override
  public int getCount() {

    return fragList.size();
  }

  @Override
  public Object instantiateItem(ViewGroup arg0, int arg1) {

    return super.instantiateItem(arg0, arg1);
  }
  @Override
  public void destroyItem(ViewGroup container, int position, Object object) {

    super.destroyItem(container, position, object);
  }

}

MainActivity裡使用getSupportFragmentManager()該Activity必須繼承FragmentActivity:

public class MainActivity extends FragmentActivity implements OnPageChangeListener{
  private List<View> viewList;
  private List<String> titleList;
  private ViewPager pager;
  private PagerTabStrip tab;//頂部標題

  private List<Fragment> fragList;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    viewList = new ArrayList<View>();
    titleList = new ArrayList<String>();
    tab = (PagerTabStrip) findViewById(R.id.tab);

    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);
    View view4 = View.inflate(this,R.layout.view4,null);

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

    fragList = new ArrayList<Fragment>();
    fragList.add(new Fragment1());
    fragList.add(new Fragment2());
    fragList.add(new Fragment3());
    fragList.add(new Fragment4());

    //為ViewPager頁卡設置標題
    titleList.add("第一頁");
    titleList.add("第二頁");
    titleList.add("第三頁");
    titleList.add("第四頁");
    //為PagerTabStrip設置一些屬性
    tab.setBackgroundColor(Color.WHITE);
    tab.setDrawFullUnderline(false);
    tab.setTabIndicatorColor(Color.BLUE);

    pager = (ViewPager) findViewById(R.id.pager);
    //MyPagerAdapter adapter = new MyPagerAdapter(viewList,titleList);
    //MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragList, titleList);
    /**
     * 使用getSupportFragmentManager()該Activity必須繼承FragmentActivity
     */
    MyFragmentPagerAdapter2 adapter = new MyFragmentPagerAdapter2(getSupportFragmentManager(), fragList, titleList);
    pager.setAdapter(adapter);
    pager.setOnPageChangeListener(this);
  }

  @Override
  public void onPageScrollStateChanged(int arg0) {

  }

  @Override
  public void onPageScrolled(int arg0, float arg1, int arg2) {

  }

  @Override
  public void onPageSelected(int arg0) {
    Toast.makeText(this,"當前是第"+(arg0+1)+"個界面", 0).show();

  }

}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved