ViewPager引导页Demo_自动循环ViewPager源码下载
程序员文章站
2022-03-01 13:23:26
...
分享一个简单的ViewPager例子、代码很简单、只有一个MainActivity、很适合刚刚使用ViewPager的哥们看、里面有内嵌一下MyAdapter、继承至PagerAdapter、另外要说明的是这个ViewPager是可以无线循环的
onCreate部分代码
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main_activity); imageView1 = (ImageView) findViewById(R.id.ad1); imageView2 = (ImageView) findViewById(R.id.ad2); imageView3 = (ImageView) findViewById(R.id.ad3); list = new ArrayList<ImageView>(); imagesid = new int[] { R.drawable.qq, R.drawable.ee, R.drawable.rr }; // imageViews = new ImageView[imagesid.length]; LinearLayout linearLayout = (LinearLayout) findViewById(R.id.viewGroup); viewPager = (ViewPager) findViewById(R.id.viewPager); for (int i = 0; i < imagesid.length; i ) { ImageView imageView = new ImageView(this); imageView.setImageResource(imagesid[i]); // imageView.setScaleType(ScaleType.CENTER_CROP); list.add(imageView); } viewPager.setAdapter(new MyAdapter()); viewPager.setOnPageChangeListener(new MyListener()); viewPager.setCurrentItem(300); mHandler.postDelayed(mRunnable, 2000); }
Adapter代码
public Object instantiateItem(View arg0, int arg1) { // TODO Auto-generated method stub try { //((ViewPager) arg0).addView(list.get(arg1),0); ((ViewPager) arg0).addView((View) list.get(arg1 % list.size()), 0); } catch (Exception e) { // TODO: handle exception } return list.get(arg1 % list.size()); }
最后给贴上Demo的源代码、希望对大家有用、有兴趣的哥们可以下载看看
源代码下载链接: http://dwtedx.com/download.html?bdkey=s/1kT7vmIV 密码: 3h6v