ViewPager讲解以及ViewFlipper

1.加入ViewPager最好导入兼容低版本

2.将布局转换为View的方法

ViewPager讲解以及ViewFlipper_第1张图片

 

3.适配器类型ViewPager讲解以及ViewFlipper_第2张图片

PagerAdapter数据适配器,以View(4个View配置文件)作为数据对象填充 
课程Demo
public class MyPagerAdapter extends PagerAdapter {

private List viewlist;
private Listtitle;
public MyPagerAdapter(List viewlist,Listtitle){
this.viewlist=viewlist;
this.title=title;
}
@Override

public int getCount() {
return viewlist.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@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));
}
@Override
public CharSequence getPageTitle(int position) {
return title.get(position);
}
FragmentPagerAdapter,以Fragment(四个Fragment类)作为填充的数据适配器
课程Demo //开发复杂的经常用到
public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
   private ListfragList;
private ListtitleList;
public MyFragmentPagerAdapter(FragmentManager fm,List fragList,ListtitleList) {
super(fm);
this.fragList=fragList;
this.titleList=titleList;
}
@Override
public Fragment getItem(int arg0) {

return fragList.get(arg0);
}
@Override
public int getCount() {
return fragList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return titleList.get(position);
}
}
FragmentStatePagerAdapter适配器多了一个创建和销毁的过程
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
}

@Override
public Object instantiateItem(ViewGroup arg0, int arg1) {
return super.instantiateItem(arg0, arg1);
}
4.设置监听器
public class MainActivity extends FragmentActivity implements OnPageChangeListener{
private ViewPager pager;private ListviewList;
private ListtitleList;
private PagerTabStrip tab;
private ListfragList;
   @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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=new ArrayList();
viewList.add(view1);
viewList.add(view2);
viewList.add(view3);
viewList.add(view4);
titleList=new ArrayList();
titleList.add("第一页");
titleList.add("第二页");
titleList.add("第三页");
titleList.add("第四页");
tab=(PagerTabStrip) findViewById(R.id.tab);
//设置背景颜色
tab.setBackgroundColor(Color.YELLOW);
//设置短下划线颜色
tab.setTabIndicatorColor(Color.BLUE);
//设置是否隐藏下划细线
tab.setDrawFullUnderline(false);
//设置字体颜色
tab.setTextColor(Color.RED);
pager=(ViewPager) findViewById(R.id.pager);
MyViewPagerAdapter adapter=new MyViewPagerAdapter(viewList, titleList);
pager.setAdapter(adapter);
fragList=new ArrayList();
fragList.add(new Fragment1());
fragList.add(new Fragment2());
fragList.add(new Fragment3());
fragList.add(new Fragment4());
// MyFragmentPagerAdapter adapter2=new MyFragmentPagerAdapter(getSupportFragmentManager(), fragList, titleList);
// pager.setAdapter(adapter2);
// MyFragmentPagerAdapter2 adapter3=new MyFragmentPagerAdapter2(getSupportFragmentManager(), fragList, titleList);
// pager.setAdapter(adapter3);
pager.setOnPageChangeListener(this);
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
Toast.makeText(this, "这是第"+(arg0+1)+"个界面", Toast.LENGTH_SHORT).show();
}
}

ViewFlipper用法:
加载的资源一般为ImageView


 
 

 

转载于:https://www.cnblogs.com/cct1314520/p/6437904.html

你可能感兴趣的:(ViewPager讲解以及ViewFlipper)