ViewPager裏實現監聽和MyFragmentStatePagerAdapter適配器和MyFragmentPagerAdapter適配器

ViewPager裏實現監聽ViewPager.OnPageChangeListener

MainActivityjava

  1 package viewpagedemo.example.administrator.viewpagedemo;
  2 
  3 import android.graphics.Color;
  4 import android.os.Bundle;
  5 import android.support.v4.app.Fragment;
  6 import android.support.v4.app.FragmentActivity;
  7 import android.support.v4.view.PagerTabStrip;
  8 import android.support.v4.view.ViewPager;
  9 import android.view.View;
 10 import android.widget.Toast;
 11 
 12 import java.util.ArrayList;
 13 import java.util.List;
 14 /*ViewPager實現(界面)導航,eg視圖滑動
 15 加入ViewPager
 16 <android.support.v4.view.ViewPager
 17 android:id="@+id/pager"
 18 android:layout_width="match_parent"
 19 android:layout_height="match_parent"
 20 android:layout_gravity="center">
 21 </android.support.v4.view.ViewPager>
 22 
 23 加載顯示的頁卡
 24 將Layout佈局轉換爲View對象
 25 1/ LayoutInflater if=getLayoutInflater().from(this);
 26 if.inflate(resource,root);
 27 2/ view.inflate(context,resource,root);
 28 
 29 ViewPager能夠當作容器能夠裝view對象或者Fragment對象
 30 
 31 配置適配器Adapter
 32 1/PagerAdapter 數據源:List<View>
 33 2/FragmentPagerAdapter 數據源: List<Fragment>
 34 3/FragmentStatePagerAdapter 數據源: List<Fragment>*/
 35 public class MainActivity extends FragmentActivity implements ViewPager.OnPageChangeListener{
 36     private PagerTabStrip tabStrip;
 37     private List<Fragment> fragmentList;
 38     private List<View>viewList;
 39     private ViewPager viewPager;
 40 
 41     private List<String> titleList;
 42     private PagerTabStrip pagerTabStrip;
 43     @Override
 44     protected void onCreate(Bundle savedInstanceState) {
 45         super.onCreate(savedInstanceState);
 46         setContentView(R.layout.main);
 47         /*經過View對象創建ViewPager的數據源*/
 48         viewList=new ArrayList<View>();
 49         View view1 = View.inflate(this, R.layout.view1, null);
 50         View view2 = View.inflate(this, R.layout.view2, null);
 51         View view3 = View.inflate(this, R.layout.view3, null);
 52         View view4 = View.inflate(this, R.layout.view4, null);
 53         viewList.add(view1);
 54         viewList.add(view2);
 55         viewList.add(view3);
 56         viewList.add(view4);
 57         /*經過Fragment做爲ViewPager的數據源*/
 58         fragmentList=new ArrayList<Fragment>();
 59 
 60         fragmentList.add(new Fragment1());
 61         fragmentList.add(new Fragment2());
 62         fragmentList.add(new Fragment3());
 63         fragmentList.add(new Fragment4());
 64         titleList=new ArrayList<String>();
 65         titleList.add("第一頁");
 66         titleList.add("第二頁");
 67         titleList.add("第三頁");
 68         titleList.add("第四頁");
 69          tabStrip= (PagerTabStrip) findViewById(R.id.paperTab);
 70         tabStrip.setBackgroundColor(Color.RED);/*設置背景顏色*/
 71         tabStrip.setTextColor(Color.YELLOW);/*設置文本顏色*/
 72         tabStrip.setDrawFullUnderline(false);/*去掉長線*/
 73         tabStrip.setTabIndicatorColor(Color.GREEN);/*設置短線顏色*/
 74 //        初始化viewPager
 75         viewPager= (ViewPager) findViewById(R.id.viewPager);
 76 //        建立PagerAdapter的適配器
 77         MyPagerAdapt adapt = new MyPagerAdapt(viewList,titleList);
 78 //        ViewPager加載適配器
 79 //        viewPager.setAdapter(adapt);
 80 //        MyFragmentPagerAdapter myFragmentPagerAdapter=new MyFragmentPagerAdapter(getSupportFragmentManager(),fragmentList,titleList);/*support v4中經過getSupportFragmentManager()獲取管理者,前提是要把繼承Activity改成繼承FragmentActivity,*/
 81 //        viewPager.setAdapter(myFragmentPagerAdapter);
 82         MyFragmentStatePagerAdapter myFragmentPagerAdapter1=new MyFragmentStatePagerAdapter(getSupportFragmentManager(),fragmentList,titleList);
 83         viewPager.setAdapter(myFragmentPagerAdapter1);
 84         viewPager.setOnPageChangeListener(this);
 85     }
 86 
 87     @Override
 88     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
 89 
 90     }
 91 
 92     @Override
 93     public void onPageSelected(int position) {/*經常使用的方法*/
 94         Toast.makeText(this,"第幾個頁面"+position,Toast.LENGTH_SHORT).show();
 95     }
 96 
 97     @Override
 98     public void onPageScrollStateChanged(int state) {
 99 
100     }
101 }

MyFragmentStatePagerAdapterandroid

 1 package viewpagedemo.example.administrator.viewpagedemo;
 2 
 3 import android.support.v4.app.Fragment;
 4 import android.support.v4.app.FragmentManager;
 5 import android.support.v4.app.FragmentPagerAdapter;
 6 import android.support.v4.app.FragmentStatePagerAdapter;
 7 import android.util.Log;
 8 import android.view.View;
 9 import android.view.ViewGroup;
10 
11 import java.util.List;
12 
13 /**
14  * Created by Administrator on 2016/5/8.
15  */
16 public class MyFragmentStatePagerAdapter extends FragmentStatePagerAdapter{
17     private List<Fragment>fragmentList;
18     private List<String>titleList;
19     public MyFragmentStatePagerAdapter(FragmentManager fm, List<Fragment>fragmentList, List<String>titleList) {
20         super(fm);
21      this.fragmentList=fragmentList;
22         this.titleList=titleList;
23     }
24 
25     @Override
26     public Fragment getItem(int position) {
27         return fragmentList.get(position);
28     }
29 
30     @Override
31     public int getCount() {
32         return fragmentList.size();
33     }
34 
35     @Override
36     public CharSequence getPageTitle(int position) {
37         return titleList.get(position);
38     }
39 
40 
41     @Override
42     public Object instantiateItem(ViewGroup container, int position) {
43         return super.instantiateItem(container, position);
44     }
45 
46     @Override
47     public void destroyItem(ViewGroup container, int position, Object object) {
48         super.destroyItem(container, position, object);
49         Log.i("nihao","nihao");
50     }
51 }

MyFragPagerAdapterapp

 1 package viewpagedemo.example.administrator.viewpagedemo;
 2 
 3 import android.support.v4.app.Fragment;
 4 import android.support.v4.app.FragmentManager;
 5 import android.support.v4.app.FragmentPagerAdapter;
 6 
 7 import java.util.List;
 8 
 9 /**
10  * Created by Administrator on 2016/5/8.
11  */
12 public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
13     private List<Fragment>fragmentList;
14     private List<String>titleList;
15     public MyFragmentPagerAdapter(FragmentManager fm,List<Fragment>fragmentList,List<String>titleList) {
16         super(fm);
17      this.fragmentList=fragmentList;
18         this.titleList=titleList;
19     }
20 
21     @Override
22     public Fragment getItem(int position) {
23         return fragmentList.get(position);
24     }
25 
26     @Override
27     public int getCount() {
28         return fragmentList.size();
29     }
30 
31     @Override
32     public CharSequence getPageTitle(int position) {
33         return titleList.get(position);
34     }
35 }
相關文章
相關標籤/搜索