<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>
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); } }
//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);