Android: ScrollView in flipper

孤者浪人 提交于 2020-01-01 20:00:48

问题


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.


回答1:


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!




回答2:


Basically, if you have a ScrollView in a ViewFlipper, all you have to do is attach onTouchListener to the ScrollView:

    ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);        
    scrollView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event){
            if (myGestureDetector.onTouchEvent(event)){
                return true;
            }
            else{   
                return false;
            }
        }
    });



回答3:


I try this way to make it works:

@Override
public boolean onTouchEvent(MotionEvent event) 
{
    scrollView.onTouchEvent(event); 
    return super.onTouchEvent(event);
}

@Override
public boolean dispatchTouchEvent(MotionEvent e)
{
    gestureDetector.onTouchEvent(e);
    return super.dispatchTouchEvent(e);
}


来源:https://stackoverflow.com/questions/3032525/android-scrollview-in-flipper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!