ViewPager的学习
参考:慕课网《Android攻城狮的第二门课》
在完成NaviActivity之前,先把需要使用的一个重要控件ViewPager学习一下。
(1)使用view作为数据源:
1 private List<View> viewList; 2 private ViewPager pager; 3 private PagerTabStrip tab; 4 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_navi); 8 Intent oldpager = getIntent(); 9 int num = oldpager.getIntExtra("Authorization", 0); 10 viewList = new ArrayList<View>(); 11 /** 12 * 通过View对象去作为ViewPager的数据源 13 */ 14 View firstView, secondView; 15 titleList = new ArrayList<String>(); 16 firstView = View.inflate(this, R.layout.activity_machine_inquiry, null); 17 titleList.add("机器查询"); 18 if (num == 0) { 19 secondView = View.inflate(this, R.layout.activity_stu_info, null); 20 //为ViewPager页卡设置标题 21 titleList.add("用户信息"); 22 }else{ 23 secondView = View.inflate(this, R.layout.activity_stu_inquiry, null); 24 titleList.add("学生查询"); 25 } 26 viewList.add(firstView); 27 viewList.add(secondView); 28 29 //为PagerTabStrip设置一些属性 30 tab = (PagerTabStrip) findViewById(R.id.stuTab); 31 tab.setBackgroundColor(Color.YELLOW); 32 tab.setTextColor(Color.RED); 33 tab.setDrawFullUnderline(false); 34 tab.setTabIndicatorColor(Color.GREEN); 35 36 //初始化ViewPager 37 pager = (ViewPager) findViewById(R.id.stuPager); 38 39 //创建PagerAdapter的适配器 40 MyPagerAdapter adapter = new MyPagerAdapter(viewList, titleList); 41 42 //ViewPager加载适配器 43 pager.setAdapter(adapter); 44 } 45 }
1 package com.example.wanderingzj.stumanage; 2 3 import android.support.v4.view.PagerAdapter; 4 import android.view.View; 5 import android.view.ViewGroup; 6 7 import java.util.List; 8 9 /** 10 * Created by samsung on 2015/10/28. 11 */ 12 public class MyPagerAdapter extends PagerAdapter { 13 private List<View> viewList; 14 private List<String> titleList; 15 16 public MyPagerAdapter(List<View> viewList, List<String> titleList){ 17 this.viewList = viewList; 18 this.titleList = titleList; 19 } 20 21 /** 22 * 返回页卡数量 23 * @return 24 */ 25 @Override 26 public int getCount() { 27 return viewList.size(); 28 } 29 30 /** 31 * View是否来自于对象 32 * @param arg0 33 * @param arg1 34 * @return 35 */ 36 @Override 37 public boolean isViewFromObject(View arg0, Object arg1) { 38 return arg0 == arg1; 39 } 40 41 /** 42 * 实例化一个页卡 43 * @param container 44 * @param position 45 * @return 46 */ 47 @Override 48 public Object instantiateItem(ViewGroup container, int position) { 49 container.addView(viewList.get(position)); 50 return viewList.get(position); 51 } 52 53 /** 54 * 销毁一个页卡 55 * @param container 56 * @param position 57 * @param object 58 */ 59 @Override 60 public void destroyItem(ViewGroup container, int position, Object object) { 61 container.removeView(viewList.get(position)); 62 } 63 64 /** 65 * 设置ViewPager页卡标题 66 * @param position 67 * @return 68 */ 69 @Override 70 public CharSequence getPageTitle(int position) { 71 return titleList.get(position); 72 } 73 }
(2)使用fragment作为数据源:
1 private List<Fragment> fragtList; 2 private ViewPager pager; 3 private PagerTabStrip tab; 4 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_navi); 8 Intent oldpager = getIntent(); 9 int num = oldpager.getIntExtra("Authorization", 0); 10 11 /** 12 * 通过fragment作为ViewPager的数据源 13 */ 14 fragtList = new ArrayList<Fragment>(); 15 //Activity向fragment传递数值 16 Bundle bundle = new Bundle(); 17 bundle.putInt("Authorization", num); 18 machineInquiryActivity = new MachineInquiryActivity(); 19 machineInquiryActivity.setArguments(bundle); 20 fragtList.add(machineInquiryActivity); 21 if (num == 0) { 22 stuInfoActivity = new StuInfoActivity(); 23 stuInfoActivity.setArguments(bundle); 24 fragtList.add(stuInfoActivity); 25 }else{ 26 stuInquiryActivity = new StuInquiryActivity(); 27 stuInquiryActivity.setArguments(bundle); 28 fragtList.add(stuInquiryActivity); 29 } 30 31 32 //为PagerTabStrip设置一些属性 33 tab = (PagerTabStrip) findViewById(R.id.stuTab); 34 tab.setBackgroundColor(Color.YELLOW); 35 tab.setTextColor(Color.RED); 36 tab.setDrawFullUnderline(false); 37 tab.setTabIndicatorColor(Color.GREEN); 38 39 //初始化ViewPager 40 pager = (ViewPager) findViewById(R.id.stuPager); 41 42 MyFragmentPagerAdapter adapterFrag = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragtList, titleList); 43 pager.setAdapter(adapterFrag); 44 } 45 }
1 package com.example.wanderingzj.stumanage; 2 3 import android.support.v4.app.FragmentPagerAdapter; 4 import android.support.v4.app.Fragment; 5 import android.support.v4.app.FragmentManager; 6 import android.support.v4.app.FragmentStatePagerAdapter; 7 import android.view.ViewGroup; 8 9 import java.util.List; 10 11 /** 12 * Created by samsung on 2015/10/28. 13 */ 14 public class MyFragmentPagerAdapter extends FragmentStatePagerAdapter { 15 private List<Fragment> fragList; 16 private List<String> titleList; 17 18 public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> fragList, List<String> titleList){ 19 super(fm); 20 this.fragList = fragList; 21 this.titleList = titleList; 22 } 23 24 @Override 25 public Fragment getItem(int position) { 26 return fragList.get(position); 27 } 28 29 @Override 30 public int getCount() { 31 return fragList.size(); 32 } 33 34 @Override 35 public CharSequence getPageTitle(int position) { 36 return titleList.get(position); 37 } 38 39 @Override 40 public Object instantiateItem(ViewGroup container, int position) { 41 return super.instantiateItem(container, position); 42 } 43 44 @Override 45 public void destroyItem(ViewGroup container, int position, Object object) { 46 super.destroyItem(container, position, object); 47 } 48 }
虽然两种方法都顺利的完成了,但在显示PageTitle那却很蛋疼,必须滑动一下才能显示出来。。。
后来经过查询得知可能是由于Support Library兼容性的关系
不过已经打算弃用PagerTabStrip 了,在navi中实现换了新的方法。