我在安卓系统中使用ScrollViews在ScrollViews中遇到了问题。我知道这不是最好的做法,但我需要让它发挥作用。
public static void disableScrollOnParentHorizontalScrollView(View v) {
v.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
requestDisallowInterceptTouchEventByDirection(v, true, HorizontalScrollView.class);
break;
case MotionEvent.ACTION_UP:
requestDisallowInterceptTouchEventByDirection(v, false, HorizontalScrollView.class);
break;
}
v.onTouchEvent(event);
return true;
}
});
}对于垂直ScrollViews,以ScrollView.class作为参数,我有与上面相同的方法。
private static void requestDisallowInterceptTouchEventByDirection(View v, boolean b, Class<?> clazz) {
while (v.getParent() != null && v.getParent() instanceof View) {
if (clazz.isInstance(v.getParent())) {
v.getParent().requestDisallowInterceptTouchEvent(b);
}
v = (View) v.getParent();
}
}这是一个截图,我想告诉你到底是什么问题。
在大多数情况下,这是可行的。

TextViews 5和9嵌套在ScrollViews中,TextViews 6和10嵌套在HorizontalScrollViews中。
因此,当它本身不能滚动时,任何视图都应该滚动它的父视图。
我的问题是,垂直滚动视图(4)内的垂直滚动视图(5)不会斥责外部垂直视图的水平滚动父(3)。
在另一个方向(View #10 (水平滚动))上的相同问题不是滚动它的父(8,水平滚动)父(4,垂直滚动),而是视图的视图(10)同时滚动,8(水平)和4(垂直)。
在某些视图上似乎有错误的拦截,但我没有发现问题。
有谁可以帮我?
诚挚的问候
发布于 2016-01-11 19:05:09
可能解决办法
好吧,要完成这个:我找到了一个可怕的解决方案,但它有效。
如果有两个嵌套的HorizontalScrollViews,我搜索第一个(或最内部的)垂直ScrollView并禁用拦截。
public static void disableScrollOnParentHorizontalScrollView(View v) {
v.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
requestDisallowInterceptTouchEventByDirection(v, true, HorizontalScrollView.class); //funzt weitgehend
if (isDirectlyHorizontalScrollViewAbove(v)) {
findFirstScrollViewAbove(v).requestDisallowInterceptTouchEvent(false);
findFirstScrollViewAbove(v).getParent().requestDisallowInterceptTouchEvent(true);
}
break;
case MotionEvent.ACTION_UP:
requestDisallowInterceptTouchEventByDirection(v, false, HorizontalScrollView.class);
break;
}
v.onTouchEvent(event);
return true;
}
});
}
private static boolean isDirectlyHorizontalScrollViewAbove(View v) {
while (v.getParent() != null && v.getParent() instanceof View) {
if (v.getParent() instanceof HorizontalScrollView) {
return true;
} else if (v.getParent() instanceof ScrollView) {
return false;
}
v = (View) v.getParent();
}
return false;
}
private static ScrollView findFirstScrollViewAbove(View v) {
while (v.getParent() != null && v.getParent() instanceof View) {
if (v.getParent() instanceof ScrollView) {
return (ScrollView) v.getParent();
}
v = (View) v.getParent();
}
return null;
}
// same method like before
private static void requestDisallowInterceptTouchEventByDirection(View v, boolean b, Class<?> clazz) {
while (v.getParent() != null && v.getParent() instanceof View) {
if (clazz.isInstance(v.getParent())) {
v.getParent().requestDisallowInterceptTouchEvent(b);
}
v = (View) v.getParent();
}
}如果有人有更好的解决方案(或者发现了我拦截的问题),请回答:)
诚挚的问候
https://stackoverflow.com/questions/34727207
复制相似问题