ScrollView下嵌套GridView或ListView默认不在顶部的解决方法
自定义ScrollView重写下面的方法
/* ScrollView下嵌套GridView或ListView默认不在顶部的解决方法*/
@Override
protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect) {
return 0;
}
例如:
package com.bearbaby.erp.staff.widget; import android.content.Context; import android.graphics.Rect; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.ViewConfiguration; import android.widget.ScrollView; /** * Created by admin on 2017/5/15. */ public class MyScrollView extends ScrollView{ private int mTouchSlop; // 上一次触摸时的X坐标 private float mPrevX; private boolean intercept;// 是否被拦截 public MyScrollView(Context context, AttributeSet attrs) { super(context, attrs); // 触发移动事件的最短距离,如果小于这个距离就不触发移动控件 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); } /* ScrollView下嵌套GridView或ListView默认不在顶部的解决方法*/ @Override protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect) { return 0; } @Override public boolean onInterceptTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mPrevX = event.getX(); intercept = true; break; case MotionEvent.ACTION_MOVE: final float eventX = event.getX(); float xDiff = Math.abs(eventX - mPrevX); // Log.d("refresh" ,"move----" + eventX + " " + mPrevX + " " + mTouchSlop); // 增加60的容差,让下拉刷新在竖直滑动时就可以触发 if (xDiff > mTouchSlop + 10) { intercept = false; } } return super.onInterceptTouchEvent(event) && intercept; } }