时间:2021-11-01 10:12:02 | 栏目:Android代码 | 点击:次
先上一张效果图:
说到ViewPager实现轮播图效果,那么肯定会用到PagerAdapter,下面先介绍下这个类。
PagerAdapter简介
PagerAdapter是Android.support.v4包中的类,是一个抽象类,直接继承于Object,导入包android.support.v4.view.PagerAdapter即可使用。
PagerAdapter主要是viewpager的适配器,而viewPager则也是在android.support.v4扩展包中新添加的一个强大的控件,可以实现控件的滑动效果,比如咱们在软件中常见的广告栏的滑动效果,用viewPager就可以实现。今天主要介绍如何使用viewPagr并重写PagerAdapter实现常见广告栏的滑动效果。
既然是个抽象类,那么我们新建一个类去继承它,重写四个方法:
1.public Object instantiateItem(ViewGroup container, int position)
2.public void destroyItem(ViewGroup container, int position, Object object)
3.public int getCount()
4.public boolean isViewFromObject(View arg0, Object arg1)
MyViewPagerAdapter类:
public class MyViewPagerAdapter extends PagerAdapter { private List<ImageView> mList; public MyViewPagerAdapter(List<ImageView> mList){ this.mList=mList; } //当要显示的图片进行缓存时,会调用这个方法进行显示图片的初始化 //我们将要显示的ImageView加入到ViewGroup中 public Object instantiateItem(ViewGroup container, int position) { // TODO Auto-generated method stub container.addView(mList.get(position)); return mList.get(position); } @Override //PagerAdapter只缓存三张要显示的图片,如果滑动的图片超出了缓存的范围,就会调用这个方法,将图片销毁 public void destroyItem(ViewGroup container, int position, Object object) { // TODO Auto-generated method stub container.removeView(mList.get(position)); } //获取要滑动的控件的数量, public int getCount() { // TODO Auto-generated method stub return mList.size(); } //来判断显示的是否是同一张照片,这个我们将两个图片对比 再返回 public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0==arg1; } }
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="200dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignBottom="@id/vp" android:background="#33000000" android:orientation="vertical" android:paddingBottom="10dp" > <LinearLayout android:id="@+id/ll_points" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="horizontal" android:paddingBottom="10dp" > </LinearLayout> </LinearLayout> </RelativeLayout>
MainActivity:
public class MainActivity extends Activity { private ViewPager mVp; //private TextView tvTitle; private LinearLayout llPoints; //private String[] titles;// 存放所有要显示的标题 private int[] images;// 存放所有要显示的图片资源id private List<ImageView> list=new ArrayList<ImageView>();;// 存放要显示在ViewPager对象中的所有Imageview对象 private int prevPosition = 0; private Handler handler = new Handler() { @SuppressLint("HandlerLeak") public void handleMessage(android.os.Message msg) { switch (msg.what) { case 0: // 得到mvp当前页面的索引 int currentItem = mVp.getCurrentItem(); // 要显示的下一个页面的索引 currentItem++; // 设置ViewPager显示的页面 mVp.setCurrentItem(currentItem % list.size()); break; default: break; } }; }; @SuppressWarnings("deprecation") protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); // titles = getTitles(); images=getImages(); for (int i = 0; i < images.length; i++) { ImageView iv=new ImageView(this); iv.setBackgroundResource(images[i]); list.add(iv); //根据图片的数量生成相对应的数量的小圆点 final View view=new View(this); view.setBackgroundResource(R.drawable.login__05); DisplayMetrics metrics=new DisplayMetrics(); float width=TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,30, metrics); float height=TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 30, metrics); LinearLayout.LayoutParams params=new LinearLayout.LayoutParams((int)width,(int)height); params.leftMargin=5; view.setLayoutParams(params); llPoints.addView(view); } //设置第一页显示的标题 //tvTitle.setText(titles[0]); //设置第一页的时候,小圆点显示的背景图 llPoints.getChildAt(0).setBackgroundResource(R.drawable.login__03); //下面封装viewpager的适配器 MyViewPagerAdapter adapter=new MyViewPagerAdapter(list); mVp.setAdapter(adapter); //设置ViewPager对象页面变化时的监听 mVp.setOnPageChangeListener(new OnPageChangeListener() { @Override //当下一个页面被选择的时候 public void onPageSelected(int arg0) { // TODO Auto-generated method stub //tvTitle.setText(titles[arg0%list.size()]); llPoints.getChildAt(prevPosition).setBackgroundResource(R.drawable.login__05); llPoints.getChildAt(arg0).setBackgroundResource(R.drawable.login__03); //把当前点位置做为下一次变化的前一个点的位置 prevPosition=arg0; } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } }); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub while (true) { SystemClock.sleep(3000); handler.sendEmptyMessage(0); } } }).start(); } private void initView() { // TODO Auto-generated method stub mVp = (ViewPager) findViewById(R.id.vp); //tvTitle = (TextView) findViewById(R.id.tv_title); llPoints = (LinearLayout) findViewById(R.id.ll_points); } private int[] getImages(){ return new int[]{R.drawable.banner_01,R.drawable.banner_02,R.drawable.banner_03}; } }