public class InnerScrollView extends ScrollView {
public InnerScrollView (Context context) {
super(context);
}
public InnerScrollView (Context context) {
super(context);
}
public InnerScrollView (Context context, AttributeSet attrs) {
super(context, attrs);
}
public InnerScrollView (Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
//在Y轴上可以滑动的最大距离 = 总长度 - 当前展示区域长度
private int maxY =0;
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
maxScrollY=getChildAt(0).getMeasuredHeight() - getHeight();
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
getParent().getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_MOVE:
if (getScrollY() > 0 && getScrollY() < maxY)
getParent().requestDisallowInterceptTouchEvent(true);
else
getParent().requestDisallowInterceptTouchEvent(false);
/*if (getScrollY()==0)
Log.i("kkk","滑到头了");
if (getChildAt(0).getMeasuredHeight() == getScrollY() + getHeight())
Log.i("kkk","滑到底了");*/
break;
case MotionEvent.ACTION_UP:
getParent().getParent().requestDisallowInterceptTouchEvent(false);
break;
}
return super.dispatchTouchEvent(ev);
}
}
这个博客展示了如何创建一个InnerScrollView,它继承自ScrollView,并在Y轴上实现滑动的最大限制。在ACTION_DOWN时,阻止父ViewGroup拦截触摸事件,允许滚动;在ACTION_MOVE和ACTION_UP时,根据滚动位置决定是否允许父ViewGroup拦截。这有助于在嵌套滚动视图中控制触摸事件的分发。
2万+

被折叠的 条评论
为什么被折叠?



