码迷,mamicode.com
首页 > 其他好文 > 详细

ViewPager里实现监听和MyFragmentStatePagerAdapter适配器和MyFragmentPagerAdapter适配器

时间:2016-05-08 18:31:05      阅读:385      评论:0      收藏:0      [点我收藏+]

标签:

ViewPager里实现监听ViewPager.OnPageChangeListener

MainActivity

  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 }

MyFragmentStatePagerAdapter

 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 }

MyFragPagerAdapter

 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 }

 

ViewPager里实现监听和MyFragmentStatePagerAdapter适配器和MyFragmentPagerAdapter适配器

标签:

原文地址:http://www.cnblogs.com/633sylss/p/5471046.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!