scrollview嵌套listview产生的滑动冲突(recyclerview)

问题描述:

布局最外层是ScrollView,里面有ListView,固定高度约400dp(加载数据后远大于400dp)

当点击到ListView区域时,事件被拦截,无法滑动ListView

解决方案:

继承ScrollView,覆写onInterceptTouchEvent方法,点击操作发生在ListView的区域的时候,返回false让ScrollView的onTouchEvent接收不到MotionEvent,而是把Event传到下一级的控件中。

外部拦截法:

public class ListScrollView extends ScrollView {

    private XListView xListView;

    public ListScrollView(Context context) {
        super(context);
    }

    public ListScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public XListView getxListView() {
        return xListView;
    }

    public void setxListView(XListView xListView) {
        this.xListView = xListView;
    }

    /**
     * 覆写onInterceptTouchEvent方法,点击操作发生在ListView的区域的时候,
     * 返回false让ScrollView的onTouchEvent接收不到MotionEvent,而是把Event传到下一级的控件中
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (xListView != null && checkArea(xListView, ev)) {
            return false;
        }
        return super.onInterceptTouchEvent(ev);
    }

    /**
     *  测试view是否在点击范围内
     * @param v
     * @return
     */
    private boolean checkArea(View view, MotionEvent event){
        // 手指点击的位置
        float currentX = event.getRawX();
        float currentY = event.getRawY();
        int[] locate = new int[2];
        view.getLocationOnScreen(locate);
        int left = locate[0];
        int right = left + v.getWidth();
        int top = locate[1];
        int bottom = top + v.getHeight();
        if (left < currentX && currentX < right && top < currentY  && currentY  < bottom) {
            return true;
        }
        return false;
    }




}

使用时,要在listscrollview中设置xlistview:

listSV.setListView(lv);

scrollview嵌套listview产生的滑动冲突(recyclerview)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值