说明:
当listview嵌套在下拉刷新中,在中部滑动ListView时候会触发下拉刷新,这样不能回到ListView的头部。
解决办法:
在ListView中监听滑动是否在头部,如果不在头部拦截触摸机制ListView自己处理行为,如果滑到了头部则放行触摸机制放行给外层下拉刷新来处理行为。
两个事件触摸传递机制供参考:
关键触摸拦截代码:
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); | 第二个是使用后的