Android:翻转器中的 ScrollView
我有一个脚蹼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/ParentLayout"
xmlns:android="http://schemas.android.com/apk/res/android" style="@style/MainLayout" >
<LinearLayout android:id="@+id/FlipperLayout" style="@style/FlipperLayout">
<ViewFlipper android:id="@+id/viewflipper" style="@style/ViewFlipper">
<!--adding views to ViewFlipper-->
<include layout="@layout/home1" android:layout_gravity="center_horizontal" />
<include layout="@layout/home2" android:layout_gravity="center_horizontal" />
</ViewFlipper>
</LinearLayout>
</LinearLayout>
第一个布局,home1,由滚动视图组成。 如何区分翻转手势和滚动手势? 目前:
- 如果我删除滚动视图,我可以滑动,
- 如果我添加滚动视图,我只能滚动。
我看到有人建议我应该重写 onInterceptTouchEvent(MotionEvent),但我不知道该怎么做。我的代码此时看起来像这样:
public class HomeActivity extends Activity {
-- declares
@Override
public void onCreate(Bundle savedInstanceState) {
-- declares & preliminary actions
LinearLayout layout = (LinearLayout) findViewById(R.id.ParentLayout);
layout.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}});
@Override
public boolean onTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
return true;
}
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// http://www.codeshogun.com/blog/2009/04/16/how-to-implement-swipe-action-in-android/
}
}
}
有人可以指导我正确的方向吗?
谢谢。
I have a flipper:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/ParentLayout"
xmlns:android="http://schemas.android.com/apk/res/android" style="@style/MainLayout" >
<LinearLayout android:id="@+id/FlipperLayout" style="@style/FlipperLayout">
<ViewFlipper android:id="@+id/viewflipper" style="@style/ViewFlipper">
<!--adding views to ViewFlipper-->
<include layout="@layout/home1" android:layout_gravity="center_horizontal" />
<include layout="@layout/home2" android:layout_gravity="center_horizontal" />
</ViewFlipper>
</LinearLayout>
</LinearLayout>
The first layout,home1, consists of a scroll view.
What should I do to distinguish between the flipping gesture and the scrolling?
Presently:
- if I remove the scroll view, I can swipe across
- if I add the scroll view, I can only scroll.
I saw a suggestion that I should override onInterceptTouchEvent(MotionEvent), but I do not know how to do this. My code, at this moment, looks like this:
public class HomeActivity extends Activity {
-- declares
@Override
public void onCreate(Bundle savedInstanceState) {
-- declares & preliminary actions
LinearLayout layout = (LinearLayout) findViewById(R.id.ParentLayout);
layout.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}});
@Override
public boolean onTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
return true;
}
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// http://www.codeshogun.com/blog/2009/04/16/how-to-implement-swipe-action-in-android/
}
}
}
Can anybody please guide me in the right direction?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
视图翻转器一次仅显示一个视图,如此处所述。当开发人员想要让用户选择如何查看数据(列表、缩略图等)时,这是一种非常有用的开关。因此,您不能同时滚动和快速滑动的原因是因为一个视图仅滚动,而另一个视图仅快速滑动,并且一次仅打开一个视图。
如果您希望显示屏同时滚动和滑动,则必须设计一个能够同时滚动和滑动的布局并覆盖所需的方法。实现这一目标的第一步是删除 ViewFLipper 并使用其他东西。
希望这有帮助!
The view flipper only displays one view at a time as explained here. It is a kind of switch that is very useful when the developer wants to give the user a choice as to how to view the data (list, thumbnails, ect). therefore, the reason why you cant scroll and fling at the same time is because one view only scrolls and the other view only flings and only one is open at a time.
If you want your display to both scroll and fling, you will have to design a layout that is capable of both and override the needed methods. The first step towards that would be to remove your ViewFLipper and use something else.
Hope this was helpful!
基本上,如果 ViewFlipper 中有一个 ScrollView,您所要做的就是将 onTouchListener 附加到 ScrollView:
Basically, if you have a ScrollView in a ViewFlipper, all you have to do is attach onTouchListener to the ScrollView:
我尝试用这种方法让它发挥作用:
I try this way to make it works: