在花费大量时间在谷歌上搜索一个相对简单的解决方案来解决我的问题时,我发现这是二维滚动的解决方案。我有一个嵌套在 ScrollView 中的水平 ScrollView 。我用几种方法摆弄了这个,但没有成功地使任何功能发挥作用。有谁对如何使这样的概念发挥作用有任何想法吗?
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/但我根本不明白如何正确实现这么长的代码。
对于我来说,二维滚动是网页 View 固有的,但在其他地方不存在的,这对我来说没有多大意义......任何和所有的帮助都是值得赞赏的。
编辑:放大图库中的图像时,它到底是如何工作的。当然,必须有一种方法可以在这里实现相同的功能。
最佳答案
我不确定您发布的博客,这是我的解决方案:
/**
* 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>
基本上,仅滚动垂直的父 ScrollView 将被禁用,因为您将使用新的自定义类。然后将 HScrollview 放入 ScrollView 中。即使 Parentscroll View 与水平 ScrollView 不垂直,它也会传递触摸,这使其具有 2D 滚动效果。
关于android - 二维滚动...需要反馈的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19411518/