抖音竖向滑动列表控件

两年没写博客了。

记录个控件,省的自己去找

竖向滑动的RecyclerView,已经在项目中用了一年半,没啥问题

1、支持预加载回调,2、position变动回调

没用androidx的重新导下包就行了


import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Author: YuJunKui
 * Time:2017/11/10 18:03
 * Tips:  特殊的像竖向的viewPage的RecyclerView
 */

public class SpecialRecyclerView extends RecyclerView {

    private final PagerSnapHelper pagerSnapHelper;
    private boolean isLoading = false;//是否正在加载更多
    private boolean loadingMoreEnabled = true;//是否开启加载更多

    private onLoadMoreListener onLoadMoreListener;
    public static final byte LOAD_MORE_VALVE_VALUE = 5;

    private static final String TAG = "SpecialRecyclerView";

    private int currentIndex;
    private boolean isFirstShow = true;

    public SpecialRecyclerView(Context context) {
        this(context, null);
    }

    public SpecialRecyclerView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public SpecialRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setLayoutManager(new LinearLayoutManager(getContext(), VERTICAL, false));
        pagerSnapHelper = new PagerSnapHelper();
        pagerSnapHelper.attachToRecyclerView(this);

        addOnChildAttachStateChangeListener(new OnChildAttachStateChangeListener() {
            @Override
            public void onChildViewAttachedToWindow(@NonNull View view) {
                if (isFirstShow) {
                    //因为检测页面变动的在滑动中 就导致了一个问题,在第一次进入页面的时候onPageChange不绘制
                    //监听view绘制后回调一次
                    isFirstShow = false;
                    if (onPageChangeListener != null) {
                        view.post(() -> onPageChangeListener.onPageChange(currentIndex));
                    }
                }
            }

            @Override
            public void onChildViewDetachedFromWindow(@NonNull View view) {

            }
        });

    }

    public int getCurrentItem() {
        return currentIndex;
    }

    public void setLoadingMoreEnabled(boolean enabled) {
        loadingMoreEnabled = enabled;
    }

    public void setOnLoadMoreListener(onLoadMoreListener onLoadMoreListener) {
        this.onLoadMoreListener = onLoadMoreListener;
    }

    public void setCurrentItem(int index) {

        if (index == currentIndex) return;

        LinearLayoutManager manager = (LinearLayoutManager) getLayoutManager();
        manager.scrollToPosition(index);
        manager.setStackFromEnd(true);

        currentIndex = index;
    }


    public void loadMoreComplete() {
        isLoading = false;
    }

    public boolean isLoading() {
        return isLoading;
    }

    @Override
    public void onScrollStateChanged(int state) {
        super.onScrollStateChanged(state);

        LayoutManager layoutManager = getLayoutManager();

        //监听加载更多
        if (loadingMoreEnabled && !isLoading && onLoadMoreListener != null) {
            int lastVisibleItemPosition = ((LinearLayoutManager) getLayoutManager()).findLastVisibleItemPosition();
            if (getLayoutManager().getChildCount() > 0
                    && lastVisibleItemPosition >= getLayoutManager().getItemCount() - LOAD_MORE_VALVE_VALUE
                    && layoutManager.getItemCount() > layoutManager.getChildCount()) {
                isLoading = true;
                onLoadMoreListener.onLoadMore();
            }
        }

        //========================================用作监听当前那个页面显示=======================================
        /**
         * 此方法在滑动过程被调用
         * 正常为
         * state值依次为SCROLL_STATE_DRAGGING(1)->SCROLL_STATE_SETTLING(2)->SCROLL_STATE_IDLE(0)
         *            拖拽->停止->稳定
         * 故当SCROLL_STATE_IDLE(0)时检测当前显示最多的是那个view
         */


        if (onPageChangeListener != null && state == SCROLL_STATE_IDLE) {

            int position = layoutManager.getPosition(pagerSnapHelper.findSnapView(layoutManager));

            if (position != currentIndex) {
                //回调出去
                currentIndex = position;
                onPageChangeListener.onPageChange(position);
            }

        }

    }

    private OnPageChangeListener onPageChangeListener;

    public void setOnPageChangeListener(OnPageChangeListener onPageChangeListener) {
        this.onPageChangeListener = onPageChangeListener;
    }

    public interface OnPageChangeListener {
        void onPageChange(int currentPosition);
    }

    public interface onLoadMoreListener {
        void onLoadMore();
    }


}

 

你可能感兴趣的:(抖音竖向滑动列表控件)