android下拉刷新和listview冲突 - listview不能下拉到头部

说明:

当listview嵌套在下拉刷新中,在中部滑动ListView时候会触发下拉刷新,这样不能回到ListView的头部。

解决办法:

在ListView中监听滑动是否在头部,如果不在头部拦截触摸机制ListView自己处理行为,如果滑到了头部则放行触摸机制放行给外层下拉刷新来处理行为。

两个事件触摸传递机制供参考:

Android 触摸事件传递机制
android事件拦截处理机制详解


关键触摸拦截代码:

getParent().requestDisallowInterceptTouchEvent(true);//拦截触摸
getParent().requestDisallowInterceptTouchEvent(false);//放行给上层,不拦截触摸


详细代码;

布局文件:

<com.baofoo.mobile.wallet.common.view.pullable.PullToRefreshLayoutView
        android:id="@+id/rv_pull"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.baofoo.pulltorefresh.activity.PullableListView
                android:id="@+id/content_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/white"
                android:divider="@color/finance_divider"/>

</com.baofoo.mobile.wallet.common.view.pullable.PullToRefreshLayoutView>

ListView文件:

PullableListView自定义ListView拦截,在onInterceptTouchEvent中拦截

/**
 * 和下拉刷新配合的listview
 */
public class PullListView extends ListView {
    public PullListView(Context context) {
        super(context);
    }

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

    public PullListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        if (getFirstVisiblePosition() == 0 && getChildAt(0).getTop() == 0) {//到头部了
            getParent().requestDisallowInterceptTouchEvent(false);//放行触摸
        } else {//没有到头部
            getParent().requestDisallowInterceptTouchEvent(true);//拦截触摸
        }
        return super.onInterceptTouchEvent(ev);
    }
}

解析:

但是listVIew必须是向上拉倒第一个item的时候才能执行下拉刷新(顺序处理002),所以要用到代码getParent().requestDisallowInterceptTouchEvent(true);

当为true时候,代表listview拦截成功,必须listView中的onTouchEvent执行完毕后设为false放行,下拉刷新才能执行

运行图:

第一个是没有使用getParent().requestDisallowInterceptTouchEvent(true);  | 第二个是使用后的

            

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Beluga_白鲸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值