ViewPager的簡單使用說明

前提:工程中使用ViewPager,須要導入google提供的jar包(android-support-v4.jar)html

要學習ViewPager的使用,建議直接看官方文檔 Creating Swipe Views with Tabsandroid

接下來主要對使用進行下總結,例子是官網上的。app

ViewPager能夠理解成一個佈局(layout)部件,如在xml中加載ide

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

每個子View做爲一個獨立的頁面,加載在ViewPager上。不過要使子View能做爲獨立頁面插入到ViewPager上,須要實現一個PagerAdapter。佈局

在PagerAdapter類或其子類中設計tab的標題(getPageTitle方法中實現)、tab的數量(在getCount方法中實現)、tab頁面的顯示內容(即fragment對象,在getItem方法中實現,該方法返回一個fragment對象)。學習

jar包中已經實現了兩種:this

1.FragmentPagerAdaptergoogle

用於tab較少、較固定的滑動。效果如圖,3個tab:spa

下面是FragmentPagerAdapter的子類的實現代碼設計

 1     public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
 2 
 3         public AppSectionsPagerAdapter(FragmentManager fm) {
 4             super(fm);
 5         }
 6 
 7         @Override
 8         public Fragment getItem(int i) {
 9             switch (i) {
10                 case 0:
11                     // The first section of the app is the most interesting -- it offers
12                     // a launchpad into the other demonstrations in this example application.
13                     return new LaunchpadSectionFragment();
14 
15                 default:
16                     // The other sections of the app are dummy placeholders.
17                     Fragment fragment = new DummySectionFragment();
18                     Bundle args = new Bundle();
19                     args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
20                     fragment.setArguments(args);
21                     return fragment;
22             }
23         }
24 
25         @Override
26         public int getCount() {
27             return 3;
28         }
29 
30         @Override
31         public CharSequence getPageTitle(int position) {
32             return "Section " + (position + 1);
33         }
34     }
FragmentPagerAdapter的子類實現

在代碼中,設置了tab的標題、tab的數量及每一個tab顯示的內容。具體fragment的實現就不貼出了,參考官網。

2.FragmentStatePagerAdapter

用於tab數量不肯定,而且當用戶切換到其餘界面時,銷燬以前的界面,達到減小內存使用。效果如圖,tab是動態可移動的:

下面是FragmentStatePagerAdapter的子類的實現代碼

 1     public static class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
 2 
 3         public DemoCollectionPagerAdapter(FragmentManager fm) {
 4             super(fm);
 5         }
 6 
 7         @Override
 8         public Fragment getItem(int i) {
 9             Fragment fragment = new DemoObjectFragment();
10             Bundle args = new Bundle();
11             args.putInt(DemoObjectFragment.ARG_OBJECT, i + 1); // Our object is just an integer :-P
12             fragment.setArguments(args);
13             return fragment;
14         }
15 
16         @Override
17         public int getCount() {
18             // For this contrived example, we have a 100-object collection.
19             return 100;
20         }
21 
22         @Override
23         public CharSequence getPageTitle(int position) {
24             return "OBJECT " + (position + 1);
25         }
26     }
FragmentStatePagerAdapter的子類實現

在代碼中,一樣設置了tab的標題、tab的數量及每一個tab顯示的內容。

 

總結步驟

1.設計佈局文件,加載ViewPager,必須包含包名:

android.support.v4.view.ViewPager

2.爲ViewPager對象設計PagerAdapter對象。該對象須要實現tab的標題內容、tab的數量及每一個頁面顯示的fragment。

3.實現所需的fragment。所實現的fragment類通常是在PagerAdapter類或其子類的getItem方法中使用。在getItem方法中會建立fragment類的對象,返回給ViewPager來顯示。getItem方法有一個參數(int position),開發時,可根據該值來肯定哪一個位置對應建立哪一個fragment對象。

 

這樣就簡單地實現了ViewPager的使用了。

相關文章
相關標籤/搜索