本文主要是介绍Android中的viewpage,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
引导页
int[] images={R.mipmap.xx,....}
List<ImageView> list=new ArrayList<>();
for(int i=0;i<images.length;i++){ImageView iv=new ImageView(this);iv.setImageResource(images[i]);list.add(iv);
}
public class MyPagerAdapter extends PagerAdapter{//适配器加载数据总条目public int getCount(){return list.size();}public boolean isViewFromObject(View view,Object object){return view==object;}//根据下标实例化public Object instantiateItem(ViewGroup container, int position) {container.addView(list.get(position));return list.get(position);}//根据下标销毁page页public void destroyItem(ViewGroup container, int position, Object object) {container.removeView(list.get(position));}}
预知有几个page页:
LinearLayout layout=(LinearLayout)findViewById(R.id.layout);
ImageView[] icons=new ImageView[viewpager数据源的长度];
for(int i=0;i<icons.length;i++){icons[i]=(ImageView)layout.getChildAt(i);icons[i].setImageResource(R.mipmap.xx);icons[i].setTag(i);icons[i].setOnClickListener(new onClickListener(){public void onClick(View v){vp.setCurrentItem((Integer)v.getTag());}}});}icons[0].setImageResource(R.mipmap.xxx);
}
page页完全动态 网路加载:
LinearLayout layout=(LinearLayout)findViewById(R.id.layout);
ImageView[] icons=new ImageView[viewpager数据源的长度];
for(int i=0;i<icons.length;i++){icons[i]=new ImageView(context);icons[i].setImageResource(R.mipmap.icon01);// android:layout_width="" android:layout_heigth=""icons[i].setLayoutParams(new ViewGroup.LayoutParams(ViewPager.LayoutParams.WRAP_CONTENT,ViewPager.LayoutParams.WRAP_CONTENT));icons[i].setMaxWidth(40);icons[i].setAdjustViewBounds(true);layout.addView(icons[i]);icons[i].setTag(i);icons[i].setOnClickListener(new onClickListener(){public void onClick(View v){vp.setCurrentItem((Integer)v.getTag());}}});
}icons[0].setImageResource(R.mipmap.xxx);
ViewPager中展示Fragment
List list=new ArrayList<>();
每个page页展示不同的fragment界面
Fragment1 f1=new Framgent1();
Fragment2 f2=new Fragment2();
list.add(f1);
list.add(f2);
每个page页中使用同一个Fragment
Bundele bundle=new Bundle();
bundle.putString(key,xxx);
Fragment1 f1=new Framgent1();
f1.setArguments(bundle);
bundle=new Bundle();
bundle.putString(key,xxx);
Fragment2 f2=new Fragment2();
f2.setArguments(bundle);
list.add(f1);
list.add(f2);
public class MyFragmentPagerAdapter extends FragmentPagerAdapter{public MyFragmentPagerAdapter(FragmentManger manger){super(manger);}public int getCount(){return list.size();}public Fragment getItem(int position){return list.get(position);}
}
这篇关于Android中的viewpage的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!