今天无意间,在listview中添加了一个viewpager作为header,发现最外层如果是SwipeToRefreshLayout的话,viewpager滑动会失常,滑动时稍微有点向下的角度,页面就会向下拉动
我的XML配置:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.witskies.appmarket.widgets.CustomSwipeToRefresh
android:id="@+id/fragment_forcus_refreshlayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/fragment_forcus_listview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</com.witskies.appmarket.widgets.CustomSwipeToRefresh>
</LinearLayout>
解决办法:
自定义一个SwipeToRefreshLayout:
public class CustomSwipeToRefresh extends SwipeRefreshLayout {
private int mTouchSlop;
private float mPrevX;
public CustomSwipeToRefresh(Context context, AttributeSet attrs) {
super(context, attrs);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mPrevX = MotionEvent.obtain(event).getX();
break;
case MotionEvent.ACTION_MOVE:
final float eventX = event.getX();
float xDiff = Math.abs(eventX - mPrevX);
if (xDiff > mTouchSlop) {
return false;
}
}
return super.onInterceptTouchEvent(event);
}
}
完美解决!