CCEMLLAC 2017-05-19 04:04 采纳率: 15.4%
浏览 1950
已采纳

只执行DOWN不走UP MOVE

如下我想在recycleview里写拦截事件处理scrollview嵌套viewpager嵌套viewpager带来的冲突问题
现在只执行DOWN不执行UP MOVE呢?

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    int action = ev.getAction();
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            downY = ev.getRawY();
            downX = ev.getRawX();
            //当传入的参数为true时,表示子组件要自己消费这次事件,告诉父组件不要拦截(抢走)这次的事件
            this.getParent().requestDisallowInterceptTouchEvent(true);
            break;
        case MotionEvent.ACTION_MOVE:
            upY = ev.getRawY();
            upX = ev.getRawX();
            floatY = downY - upY;
            floatX = downX - upX;
            if (Math.abs(floatX) > 10) {
                this.getParent().requestDisallowInterceptTouchEvent(false);
            }
            //上滑
            if (floatY > 0) {
                this.addOnScrollListener(new OnScrollListener() {
                    @Override
                    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                        super.onScrolled(recyclerView, dx, dy);
                        //获取可见数目
                        layoutManager = (LinearLayoutManager) getLayoutManager();
                        visibleItemCount = layoutManager.getChildCount();
                        LogUtils.i("可见数目 " + visibleItemCount);
                    }
                });
                if (visibleItemCount > 5) {
                    this.getParent().requestDisallowInterceptTouchEvent(true);
                } else {
                    this.getParent().requestDisallowInterceptTouchEvent(false);
                }
            }
            //下滑
            else {
                this.addOnScrollListener(new OnScrollListener() {
                    @Override
                    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                        super.onScrolled(recyclerView, dx, dy);
                        //获取recycleview首个item是否可见
                        layoutManager = (LinearLayoutManager) getLayoutManager();
                        seeFirstPostion = layoutManager.findFirstCompletelyVisibleItemPosition();
                        mmPosition = layoutManager.findFirstVisibleItemPosition();
                        seeLastPosition = layoutManager.findLastCompletelyVisibleItemPosition();
                        LogUtils.i("可见" + seeFirstPostion + "\n" + mmPosition + "\n" + seeLastPosition);
                    }
                });

                //如果第一条不可见
                if (seeFirstPostion > 0) {
                    this.getParent().requestDisallowInterceptTouchEvent(true);
                } else {
                    this.getParent().requestDisallowInterceptTouchEvent(false);
                }
            }
            break;
        case MotionEvent.ACTION_UP:
            this.getParent().requestDisallowInterceptTouchEvent(false);
            break;
    }
    super.onTouchEvent(ev);
    return false;
}

  • 写回答

1条回答 默认 最新

  • CCEMLLAC 2017-05-19 05:39
    关注

    呃呃呃自问自答来了:
    原因终于找到,要在此Recycleview中的item根布局中添加上下面几句代码来让其可点可获取焦点:
    android:focusableInTouchMode="true"
    android:clickable="true"
    android:focusable="true"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?