虽然花了大量时间谷歌搜索我的问题的相对简单的解决方案,我发现这是二维滚动的解决方案.我有一个嵌套在scrollview中的horizontalscrollview.我在几个方面摆弄了这个并且没有成功地制作任何功能.有没有人对如何制作这样的概念有任何想法?
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/但是我根本不明白如何正确地实现这么长的代码.
对我来说,二维滚动是webview中固有的但在其他地方不存在对我来说没有多大意义……任何和所有的帮助都是值得赞赏的.
编辑:放大图库中的图像时,这是如何工作的.当然,必须有一种方法来实现相同的功能.
解决方法:
/**
* 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>
基本上,仅滚动veritcal的父滚动视图将被禁用,因为您将使用新的自定义类.然后在滚动视图中放置HScrollview.即使父级滚动视图不垂直于使其成为2D滚动效果的horiszontalscroll视图,它也会通过触摸.