1. 程式人生 > >RecyclerView實現類似ViewPager翻頁OnPageChangeListener監聽功能

RecyclerView實現類似ViewPager翻頁OnPageChangeListener監聽功能

轉載請註明:https://blog.csdn.net/u012854870/article/details/84984066

我們在使用ViewPager的時候多多少少都會遇到一些坑,這裡今天就不細說了。

 

首先我們先介紹一下SnapHelper

Google 在 Android 24.2.0 的support 包中添加了SnapHelper,SnapHelper是對RecyclerView的拓展,結合RecyclerView使用,能很方便的做出一些炫酷的效果。SnapHelper到底有什麼功能呢?SnapHelper旨在支援RecyclerView的對齊方式,也就是通過計算對齊RecyclerView中TargetView 的指定點或者容器中的任何畫素點。

接下來介紹一下SnapHelper的兩個子類,LinearSnapHelper和PagerSnapHelper,我們要實現ViewPaper就需要用到其中的某一個

(1) LinearSnapHelper
LinearSnapHelper 使當前Item居中顯示,類似ViewPager效果,但是又可以快速滑動(滑動多頁)。程式碼如下

 LinearSnapHelper linearSnapHelper = new LinearSnapHelper();
 //將SnapHelper attach 到RecyclrView
 linearSnapHelper.attachToRecyclerView(recyclerView);

2) PagerSnapHelper (在Android 25.1.0 support 包加入的)
PagerSnapHelper的展示效果和LineSnapHelper是一樣的,只是PagerSnapHelper 限制一次只能滑動一頁,不能快速滑動。程式碼如下:

PagerSnapHelper pagerSnapHelper = new PagerSnapHelper();
//將SnapHelper attach 到RecyclrView
pagerSnapHelper.attachToRecyclerView(recyclerView);

至此我們可以根據自己的需求選擇合適的SnapHelper實現ViewPager翻頁效果。

接下來進入今天的主題RecyclerView新增翻頁監聽功能

ViewPager本身幫我們實現了OnPageChangeListener我們只需要新增就可以,而我們使用RecycleView實現的ViewPager效果本身是沒有OnPageChangeListener事件的實現的,我們可以利用RecyclerView.OnScrollListener實現此功能。程式碼如下:

/**
 * Created by pangli on 2018/12/13 9:43
 * 備註:RecyclerView實現類似ViewPager的PageChangeListener監聽
 */
public class RecyclerViewPageChangeListenerHelper extends RecyclerView.OnScrollListener {
    private SnapHelper snapHelper;
    private OnPageChangeListener onPageChangeListener;
    private int oldPosition = -1;//防止同一Position多次觸發

    public RecyclerViewPageChangeListenerHelper(SnapHelper snapHelper, OnPageChangeListener onPageChangeListener) {
        this.snapHelper = snapHelper;
        this.onPageChangeListener = onPageChangeListener;
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        if (onPageChangeListener != null) {
            onPageChangeListener.onScrolled(recyclerView, dx, dy);
        }
    }

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        int position = 0;
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        //獲取當前選中的itemView
        View view = snapHelper.findSnapView(layoutManager);
        if (view != null) {
            //獲取itemView的position
            position = layoutManager.getPosition(view);
        }
        if (onPageChangeListener != null) {
            onPageChangeListener.onScrollStateChanged(recyclerView, newState);
            //newState == RecyclerView.SCROLL_STATE_IDLE 當滾動停止時觸發防止在滾動過程中不停觸發
            if (newState == RecyclerView.SCROLL_STATE_IDLE && oldPosition != position) {
                oldPosition = position;
                onPageChangeListener.onPageSelected(position);
            }
        }
    }

    public interface OnPageChangeListener {
        void onScrollStateChanged(RecyclerView recyclerView, int newState);

        void onScrolled(RecyclerView recyclerView, int dx, int dy);

        void onPageSelected(int position);
    }

}
至此我們就實現了RecyclerView的OnPageChangeListener功能。

使用:

        LinearSnapHelper linearSnapHelper = new LinearSnapHelper();
        linearSnapHelper.attachToRecyclerView(recyclerView);
        recyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                recyclerView.smoothScrollToPosition(4);
            }
        }, 5000);
        recyclerView.addOnScrollListener(new RecyclerViewPageChangeListenerHelper(linearSnapHelper,
                new RecyclerViewPageChangeListenerHelper.OnPageChangeListener() {


                    @Override
                    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

                    }

                    @Override
                    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

                    }

                    @Override
                    public void onPageSelected(int position) {
                        ToastUtils.showToastShort(mContext, "選中" + position);
                        Log.e("Zorro", "選中" + position);
                    }
                }));