Android - ViewPager

【Android - ViewPager】胸怀万里世界, 放眼无限未来。这篇文章主要讲述Android - ViewPager相关的知识,希望能为你提供帮助。
ViewPager (视图滑动切换工具)

< android.support.v4.view.ViewPager android:id=" @+id/viewPager" android:layout_centerHorizontal=" true" android:layout_width=" 230dp" android:layout_height=" 150dp" > < /android.support.v4.view.ViewPager>

PagerAdapter 适配器
class MyPagerAdapter extends PagerAdapter {public List< View> vieLists; public MyPagerAdapter(List< View> vieLists) { this.vieLists = vieLists; } @Override public int getCount() { return vieLists.size(); }@Override public boolean isViewFromObject(View view, Object object) { return view==object; }@Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(vieLists.get(position)); 、 } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(vieLists.get(position)); //每次滑动返回一个View return vieLists.get(position); } }

为ViewPager设置适配器:
//ViewPget ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager); List< View> list=new ArrayList< > (); ImageView iv1 = new ImageView(this); Glide.with(this).load(R.drawable.shanghai1).into(iv1); ImageView iv2 = new ImageView(this); Glide.with(this).load(R.drawable.shanghai2).into(iv2); ImageView iv3 = new ImageView(this); Glide.with(this).load(R.drawable.shanghai3).into(iv3); // 直接添加ImageView list.add(iv1); list.add(iv2); list.add(iv3); // 添加布局View //LayoutInflater li=getLayoutInflater(); //list.add(li.inflate(R.layout.tab1_layout,null,false)); //list.add(li.inflate(R.layout.tab2_layout,null,false)); //设置适配器 MyPagerAdapter adapter=new MyPagerAdapter(list); viewPager.setAdapter(adapter);


    推荐阅读