Android----ViewPager的无限轮播

目录

无标题.png

思路

1.重写viewPage, 传入(一组view,间隔时间,一个现在所处位置改变事件)作为参数
2.用一个线程去管理viewpager的自己滑动
3.为了解决手势和自动的冲突,出现手势状态改变,就去处理线程。

代码

OnViewpagerChangeListener.java
public interface OnViewpagerChangeListener {

    void onChange(int currentPage);
}
ScrollerViewpager.java
public class ScrollerViewpager extends ViewPager {

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 100002)
                setCurrentItem(getCurrentItem() + 1);
        }
    };

    //滚动的页面
    private List views;

    //滚动的页面数量
    private int count;

    //自动滚动信号
    private final int SCROLL = 100002;

    //创建一个滚动线程
    private Thread thread;

    //间隔时间
    private int SPACE;

    //目前线程状态
    private boolean NORMAL = true;

    public ScrollerViewpager(@NonNull Context context) {
        super(context);
    }

    public ScrollerViewpager(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 功能:初始化
     * @param views 视图组
     * @param space 间隔时间
     * @param listener 监听事件
     */
    public void init(List views, final int space, final OnViewpagerChangeListener listener){
        this.views = views;
        this.count = views.size();
        this.SPACE = space;

        setAdapter(new MyAdapter());
        setCurrentItem(Integer.MAX_VALUE / 2 - Integer.MAX_VALUE / 2 % views.size());

        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    try {
                        NORMAL = true;
                        Thread.sleep(SPACE * 1000);
                        handler.sendEmptyMessage(SCROLL);
                    } catch (InterruptedException e) {
                        try {
                            NORMAL = false;
                            Thread.sleep(Integer.MAX_VALUE);
                        } catch (InterruptedException e1) { }
                    }
                }
            }
        });
        thread.start();

        addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                listener.onChange(position % count);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if ((!NORMAL && state != 1) || (NORMAL && state == 1)){
                    thread.interrupt();
                }
            }
        });
    }

    public class MyAdapter extends PagerAdapter{

        @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) {
            int temp = position % count;
            View view = views.get(temp);
            if (view.getParent() == container) {
                container.removeView(view);
            }
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
        }
    }
}

使用方法

布局


    
java

public class MainActivity extends AppCompatActivity {

    private ScrollerViewpager viewpager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewpager = findViewById(R.id.viewpager);
        ArrayList views = new ArrayList<>();
        views.add(LayoutInflater.from(this).inflate(R.layout.item, null,false));
        views.add(LayoutInflater.from(this).inflate(R.layout.item1, null,false));
        views.add(LayoutInflater.from(this).inflate(R.layout.item2, null,false));

        final Button btn1 = findViewById(R.id.button1);
        final Button btn2 = findViewById(R.id.button2);
        final Button btn3 = findViewById(R.id.button3);

        viewpager.init(views, 4, new OnViewpagerChangeListener() {
            @Override
            public void onChange(int currentPage) {
                switch (currentPage){
                    case 0:
                        btn1.setBackgroundColor(Color.YELLOW);
                        btn2.setBackgroundColor(Color.WHITE);
                        btn3.setBackgroundColor(Color.WHITE);
                        break;
                    case 1:
                        btn1.setBackgroundColor(Color.WHITE);
                        btn2.setBackgroundColor(Color.YELLOW);
                        btn3.setBackgroundColor(Color.WHITE);
                        break;
                    case 2:
                        btn1.setBackgroundColor(Color.WHITE);
                        btn2.setBackgroundColor(Color.WHITE);
                        btn3.setBackgroundColor(Color.YELLOW);
                        break;
                }
            }
        });
    }
}

demo

https://github.com/pgyCode/ScrollerViewpager

你可能感兴趣的:(Android----ViewPager的无限轮播)