虽然花了大量时间在谷歌上搜索一个相对简单的解决方案来解决我的问题,但我发现这是二维滚动的解决方案。 我有一个嵌套在滚动视图中的水平滚动视图。 我以几种方式摆弄了这个,但没有成功地使任何功能。 有没有人对如何使这样的概念发挥作用有任何想法?
Scrollview scrollY = (ScrollView)findViewById(R.id.scrollY);
LinearLayout scrollYChild = (LinearLayout)findViewById(R.id.scrollYChild);
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
scrollYChild.dispatchTouchEvent(event);
scrollY.onTouchEvent(event);
return true;
}
我也发现了这个:http://blog.gorges.us/2010/06/android-two-dimensional-scrollview/但我完全不明白如何正确实现这么长的一段代码。
二维滚动是网页视图中固有的但在其他地方不存在,这对我来说没有多大意义......感谢任何和所有的帮助。
编辑:放大图库中的图像时,这究竟是如何工作的。 当然,必须有一种方法来实现相同的功能。
我不确定您发布的博客,这是我的解决方案:
/**
* This class disables Y-motion on touch event.
* It should only be used as parent class of HorizontalScrollView
*/
public class ParentScrollView extends ScrollView {
private GestureDetector mGestureDetector;
View.OnTouchListener mGestureListener;
@SuppressWarnings("deprecation")
public ParentScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
mGestureDetector = new GestureDetector(new YScrollDetector());
setFadingEdgeLength(0);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if( mGestureDetector.onTouchEvent(ev)&super.onInterceptTouchEvent(ev)){
return true;
}else{
return false;
}
}
// Return false if we're scrolling in the x direction
class YScrollDetector extends SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if(Math.abs(distanceY) > Math.abs(distanceX)) {
return true;
}
return false;
}
}
}
.XML:
<com.example.Views.ParentScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<HorizontalScrollView
android:id="@+id/tlDBtable"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</HorizontalScrollView>
</com.example.Views.ParentScrollView>
基本上,仅滚动的父滚动视图将被禁用,因为您将使用新的自定义类。然后,您将HScrollview放在滚动视图中。父滚动视图将传递触摸,即使它不垂直于horiszontalscroll视图,这使其成为2D滚动效果。