虽然使用 onTouchEvent,但 ViewFlipper 中的 ScrollView 不起作用
在这篇文章中: Android:flipper中的ScrollView
建议为ScrollView设置一个onTouchnListener是 ViewFlipper 子级的一部分。
我这样做了,并且也使用了相同的逻辑:
public class MainActivity extends Activity implements OnGestureListener {
// ...
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
this.gestureDetector = new GestureDetector(this);
// ..
createViews();
}
private void createViews() {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
displayView = inflater.inflate(R.layout.viewflippercontent, null);
ScrollView scrollView = (ScrollView) displayView
.findViewById(R.id.scrollview);
scrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
Log.d("ScrollViewOnTouchListener", "true");
return true;
} else {
Log.d("ScrollViewOnTouchListener", "false");
return false;
}
}
});
// ..
}
@Override
public boolean onTouchEvent(MotionEvent me) {
Log.d("onTouchEvent", "onTouchEvent");
return gestureDetector.onTouchEvent(me);
}
public boolean onDown(MotionEvent e) {
return true;
}
public void onLongPress(MotionEvent e) {
}
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
Log.d("onScroll", "onScroll");
return false;
}
public void onShowPress(MotionEvent e) {
}
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
}
当我在 ViewFlipper 的子级中滚动时,我在 LogCat 中得到以下内容:
08-07 21:57:14.581: DEBUG/ScrollViewOnTouchListener(4586): true
08-07 21: 57:14.591:调试/ScrollViewOnTouchListener(4586):假
08-07 21:57:14.622:调试/ ScrollViewOnTouchListener(4586):假
08-07 21:57:14.646:调试/ onScroll(4586):onScroll
08-07 21:57:14.646:调试/ ScrollViewOnTouchListener(4586):错误的
08-07 21:57:14.671:调试/ onScroll(4586):onScroll
08-07 21:57:14.671:调试/ ScrollViewOnTouchListener(4586):假
08-07 21:57:14.704:调试/ onScroll(4586):滚动时
08-07 21:57:14.704:调试/ ScrollViewOnTouchListener(4586):假
08-07 21:57:14.731:调试/ onScroll(4586):onScroll
08-07 21:57:14.731:调试/ ScrollViewOnTouchListener(4586):错误的
08-07 21:57:14.761:调试/ onScroll(4586):onScroll
08-07 21:57:14.761:调试/ ScrollViewOnTouchListener(4586):假
08-07 21:57:14.791:调试/ onScroll(4586):滚动时
08-07 21:57:14.791: DEBUG/ScrollViewOnTouchListener(4586): false
08-07 21:57:14.791: DEBUG/ScrollViewOnTouchListener(4586): true
但是没有发生滚动,尽管应该是可能的,因为内容大于屏幕。
我做错了什么?
In this post:
Android: ScrollView in flipper
It was suggested to set an onTouchnListener to the ScrollView which is part of a child of a ViewFlipper.
I did that and also used the same logic:
public class MainActivity extends Activity implements OnGestureListener {
// ...
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
this.gestureDetector = new GestureDetector(this);
// ..
createViews();
}
private void createViews() {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
displayView = inflater.inflate(R.layout.viewflippercontent, null);
ScrollView scrollView = (ScrollView) displayView
.findViewById(R.id.scrollview);
scrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
Log.d("ScrollViewOnTouchListener", "true");
return true;
} else {
Log.d("ScrollViewOnTouchListener", "false");
return false;
}
}
});
// ..
}
@Override
public boolean onTouchEvent(MotionEvent me) {
Log.d("onTouchEvent", "onTouchEvent");
return gestureDetector.onTouchEvent(me);
}
public boolean onDown(MotionEvent e) {
return true;
}
public void onLongPress(MotionEvent e) {
}
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
Log.d("onScroll", "onScroll");
return false;
}
public void onShowPress(MotionEvent e) {
}
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
}
When I scroll in the child of the ViewFlipper I get the following in the LogCat:
08-07 21:57:14.581: DEBUG/ScrollViewOnTouchListener(4586): true
08-07 21:57:14.591: DEBUG/ScrollViewOnTouchListener(4586): false
08-07 21:57:14.622: DEBUG/ScrollViewOnTouchListener(4586): false
08-07 21:57:14.646: DEBUG/onScroll(4586): onScroll
08-07 21:57:14.646: DEBUG/ScrollViewOnTouchListener(4586): false
08-07 21:57:14.671: DEBUG/onScroll(4586): onScroll
08-07 21:57:14.671: DEBUG/ScrollViewOnTouchListener(4586): false
08-07 21:57:14.704: DEBUG/onScroll(4586): onScroll
08-07 21:57:14.704: DEBUG/ScrollViewOnTouchListener(4586): false
08-07 21:57:14.731: DEBUG/onScroll(4586): onScroll
08-07 21:57:14.731: DEBUG/ScrollViewOnTouchListener(4586): false
08-07 21:57:14.761: DEBUG/onScroll(4586): onScroll
08-07 21:57:14.761: DEBUG/ScrollViewOnTouchListener(4586): false
08-07 21:57:14.791: DEBUG/onScroll(4586): onScroll
08-07 21:57:14.791: DEBUG/ScrollViewOnTouchListener(4586): false
08-07 21:57:14.791: DEBUG/ScrollViewOnTouchListener(4586): true
However no scrolling takes place although it should be possible since the content is bigger than the screen.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您的问题与这篇文章中发现的问题类似:当滚动时间太短而无法滚动时,Scrollview 不会滑动
看一下建议的解决方案,即作者扩展 ScrollView 视图类的解决方案。我相信这会解决你的问题
I think you're problem is similar to one found in this post:Scrollview doesn't swipe when it's too short to scroll
take a look at the proposed solution, the one where the author extended the ScrollView view class. i believe this will fix your pro
在此链接找到解决方案 -> 在RelativeLayout中使用ScrollView的片段> ontouch 不起作用
将以下代码添加到滚动视图内的视图中
Found a solution at this link -> Fragment using ScrollView inside RelativeLayout > ontouch doesn't work
Add the following code to your view inside scrollview