美文网首页
recycleView 横向滚动+item中包含纵向滚动+整页滚

recycleView 横向滚动+item中包含纵向滚动+整页滚

作者: 风___________ | 来源:发表于2019-03-20 14:31 被阅读0次

    第一步解决横纵滚动嵌套的问题:

    拦截法:
    外层的recycleView拦截滚动
    1.判断(横向滚动的距离>纵向)则自己消费滚动

    1. 判断(横向滚动的距离<纵向)则传递滚动
    /**
     * 处理滚动冲突的recycleview
     * */
    public class KapTouchRecycleView extends RecyclerView {
        private static final String TAG = "KapTouchRecycleView";
    
        private int mLastXIntercept;
        private int mLastYIntercept;
    
        public KapTouchRecycleView(Context context) {
            super(context);
        }
    
        public KapTouchRecycleView(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }
        // 重新滚动冲突
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            boolean intercepted = false;
            int x = (int) ev.getX();
            int y = (int) ev.getY();
            final int action = ev.getAction() & MotionEvent.ACTION_MASK;
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    intercepted = false;
                    //调用ViewPager的onInterceptTouchEvent方法初始化mActivePointerId
                    super.onInterceptTouchEvent(ev);
                    break;
                case MotionEvent.ACTION_MOVE:
                    //横坐标位移增量
                    int deltaX = x - mLastXIntercept;
                    //纵坐标位移增量
                    int deltaY = y - mLastYIntercept;
                    if (Math.abs(deltaX)>Math.abs(deltaY)){
                        intercepted = true;
                    }else{
                        intercepted = false;
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    intercepted = false;
                    break;
                default:
                    break;
            }
    
            mLastXIntercept = x;
            mLastYIntercept = y;
            return intercepted;
        }
    }
    

    第二步:解决整页滚动问题:

    {
                PagerSnapHelper snapHelper = new PagerSnapHelper();
                snapHelper.attachToRecyclerView(recyclerView);
    }
    
    

    第三部预加载:

    思路:当前位置如果临街数组边界,则触发网络请求,预加载数据

    如何获取当前位置:

            itemWidth = KapViewFrameSetingHelper.getScreenWidth(getContext()) - (white_view_left_offset*2);  // 此处根据实际显示宽度,自己计算recycleview的显示宽度
    
    // 滚动监听 && 累加记录dx
    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                        super.onScrollStateChanged(recyclerView, newState);
                        if (newState == RecyclerView.SCROLL_STATE_IDLE){
                            int currentIndex = (overallXScroll+10)/itemWidth;
                            if (currentIndex < 3) loadBeforeModelDatas();
                            if ((contentLists.size() - currentIndex) < 3) loadAfterModelDatas();
                        }
                    }
    
                    @Override
                    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                        super.onScrolled(recyclerView, dx, dy);
                    overallXScroll += dx; // 记录滚动偏移
                    }
                });
    // loadBeforeModelDatas  别忘了追加到overallXScroll
    overallXScroll += (itemWidth*models.size());// 追加一下整页滚动的偏移
    // 如果有item的删除也别忘了 更改overallXScroll
    if (position < (overallXScroll+10)/itemWidth) overallXScroll -= itemWidth;
    

    相关文章

      网友评论

          本文标题:recycleView 横向滚动+item中包含纵向滚动+整页滚

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