美文网首页
安卓爬坑指南之Recylerview

安卓爬坑指南之Recylerview

作者: tanbiheng | 来源:发表于2018-03-17 10:53 被阅读0次

    本文主要针对两个问题进行说明:

    • 横向rv和viewpager滚动冲突
    • rv滚动到底部监听

    首先我们写一个自定义的rv

    public class DefRecylerView extends RecyclerView {
        
        public DefRecylerView(Context context) {
            super(context);
        }
    
        public DefRecylerView(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }
    
        public DefRecylerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    }
    
    1. rv、viewpager滚动冲突

    当遇到外层viewpager里面有一个横向滚动的rv,为防止rv滚动到最右侧时触发viewpager的滚动我们需要这样做:

        private ViewPager viewPager = null;
    
        public void setViewPager(ViewPager viewPager) {
            this.viewPager = viewPager;
        }
    
        @Override
        public boolean dispatchTouchEvent(MotionEvent ev) {
            if (viewPager != null) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            return super.dispatchTouchEvent(ev);
        }
    

    我们在自定义的rv中重写dispatchTouchEvent方法,最为核心的一句代码为:
    getParent().requestDisallowInterceptTouchEvent(true);
    意思是当外层view是viewpager时,我们取消viewpager对滚动事件的拦截,让滚动事件继续往下传递到rv,从而实现了横向rv滚动到最右侧时不触发viewpager的滚动。

    1. rv滚动到底部监听

    自定义rv中重写onScroll方法,定义一个回调接口,当rv滚动到底部时回调通知,适用于列表数据加载更多操作。

    回调接口:

    public interface OnSlideBottomListener {
        /**
         * 滚动到底部
         */
        void onSlideBottom();
    }
    

    重写onScroll:

        private OnSlideBottomListener onSlideBottomListener = null;
    
        public void setOnSlideBottomListener(OnSlideBottomListener onSlideBottomListener) {
            this.onSlideBottomListener = onSlideBottomListener;
        }
        
        @Override
        public void onScrolled(int dx, int dy) {
            if (isSlideToBottom()) {
                if (onSlideBottomListener != null) {
                    onSlideBottomListener.onSlideBottom();
                }
            }
        }
    
        /**
         * 是否滑动到底部
         */
        public boolean isSlideToBottom() {
            if (this.getLayoutManager() instanceof LinearLayoutManager) {
                LinearLayoutManager layoutManager = (LinearLayoutManager) this.getLayoutManager();
                if (layoutManager.getOrientation() == LinearLayoutManager.VERTICAL) {
                    return this != null
                            && this.computeVerticalScrollExtent() + this.computeVerticalScrollOffset()
                            >= this.computeVerticalScrollRange();
                } else {
                    return this != null
                            && this.computeHorizontalScrollExtent() + this.computeHorizontalScrollOffset()
                            >= this.computeHorizontalScrollRange();
                }
            } else if(this.getLayoutManager() instanceof StaggeredGridLayoutManager){
                StaggeredGridLayoutManager layoutManager = (StaggeredGridLayoutManager) this.getLayoutManager();
                if (layoutManager.getOrientation() == StaggeredGridLayoutManager.VERTICAL) {
                    return this != null
                            && this.computeVerticalScrollExtent() + this.computeVerticalScrollOffset()
                            >= this.computeVerticalScrollRange();
                } else {
                    return this != null
                            && this.computeHorizontalScrollExtent() + this.computeHorizontalScrollOffset()
                            >= this.computeHorizontalScrollRange();
                }
            }
    
            return false;
        }
    

    相关文章

      网友评论

          本文标题:安卓爬坑指南之Recylerview

          本文链接:https://www.haomeiwen.com/subject/opzlqftx.html