ViewPager无限轮播图

时间:2022-05-02 10:12:16

一、布局

<FrameLayout  android:layout_width="match_parent"  android:layout_height="wrap_content">

        <android.support.v4.view.ViewPager  android:layout_width="match_parent"  android:layout_height="200dp"  android:id="@+id/pager"></android.support.v4.view.ViewPager>
添加小圆点
    <LinearLayout  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:id="@+id/linear"  android:orientation="horizontal"  android:gravity="center"  android:layout_gravity="bottom"  ></LinearLayout>
</FrameLayout>

二、获取图片

private Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        int currentItem = pager.getCurrentItem();
        currentItem++;
    改变小圆点的选择
        setyuanselect(currentItem%img.size());
        pager.setCurrentItem(currentItem);
        sendEmptyMessageDelayed(0,1000);
    }
};

private void setpager() {

    img.add("http://img3.utuku.china.com/480x0/news/20170331/5228a6f9-5b0a-431a-b7b6-1d712ab7c86d.jpg");
    img.add("http://s16.sinaimg.cn/mw690/001pPXi2gy6W1BdDgujcf&690");
    img.add("http://img1.gtimg.com/ent/pics/hv1/213/155/1650/107330988.jpg");
    img.add("http://mat1.gtimg.com/ent/109a/64004.jpg");

    MyPagerAdapter adapter1 = new MyPagerAdapter(MainActivity.this,img);
    pager.setAdapter(adapter1);
    handler.sendEmptyMessageDelayed(0,1000);

}
自定义MyPagerAdapter类
public class MyPagerAdapter extends PagerAdapter {
    private final DisplayImageOptions options;
    private Context context;
    private List<String> img;

    public MyPagerAdapter(Context context, List<String> img) {
        this.context = context;
        this.img = img;
       
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = new ImageView(context);
        ImageLoader.getInstance().displayImage(img.get(position%img.size()),imageView,options);
        container.addView(imageView);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
       container.removeView((View) object);
    }
}
三、设置小圆点
 
private List<ImageView> posd = new ArrayList<>();
 
private void inityuandian() {
    for (int i=0;i<img.size();i++){
        ImageView imageView = new ImageView(MainActivity.this);
        if (i==0){
            imageView.setImageResource(R.drawable.point_selected);
        }else{
            imageView.setImageResource(R.drawable.point_mormal);
        }
        linear.addView(imageView);
        //添加到集合中  posd.add(imageView);
    }
}
四、设置当前选中的小圆点
public void setyuanselect(int yuanselect) {
 for (int i=0;i<posd.size();i++){
      if (i==yuanselect){
            posd.get(i).setImageResource(R.drawable.point_selected);
      }else{
          posd.get(i).setImageResource(R.drawable.point_mormal);
      }
  }
}