在onTouchListener上苦苦挣扎

问题描述 投票:1回答:2

我有一个ViewFlipper,我想设置一个touchListener。它位于ScrollView内部(在ViewFlipper下面有更多UI元素),我希望如果用户进行垂直移动,则滚动视图起作用,当用户进行水平移动时,ViewFlipper会动作。 (组件在ViewFlipper中动态添加)。

为了澄清,这是布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/parentrl">



<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ViewFlipper
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/vflipper"
            android:layout_marginStart="20dp"
            android:layout_marginEnd="20dp"
            android:layout_marginTop="20dp"
            android:background="@drawable/flightcomponentborder">


        </ViewFlipper>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/inforecogida"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            android:text="@string/inforecogida"
            android:textColor="@color/actionbardefaultcolor"
            android:layout_below="@+id/vflipper"/>

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_below="@+id/inforecogida"
            android:id="@+id/separador"
            android:background="@color/gris"
            android:layout_marginTop="10dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/personaheader"
            android:layout_below="@+id/separador"
            android:layout_marginTop="10dp"
            android:layout_alignStart="@+id/inforecogida"
            android:text="@string/persona"
            android:textColor="@color/actionbardefaultcolor"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/persona"
            android:layout_below="@+id/personaheader"
            android:layout_alignStart="@+id/inforecogida"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/infotext"
            android:layout_alignStart="@+id/inforecogida"
            android:layout_below="@+id/persona"
            android:text="@string/infotext"
            android:textStyle="italic"/>

    </RelativeLayout>

</ScrollView>



</RelativeLayout>

这是我添加到ViewFlipper的TouchListener:

parentListener = new View.OnTouchListener(){
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getActionMasked();
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    startX = event.getX();
                    startY = event.getRawY();

                    break;


                case MotionEvent.ACTION_UP:

                    endX = event.getX();
                    endY = event.getRawY();
                    int movX;//0=swipe izq 1=swipe right
                    int movY;
                    if(startX-endX < 0){
                        movX=0;
                        Log.i("David", "Swipe left action up");
                        float totalPixelsX=Math.abs(startX-endX);
                        float totalPixelsY=event.getRawY() - startY;
                        Log.i("David", "totalPixelsx: "+totalPixelsX);
                        Log.i("David", "totalPixelsY: "+totalPixelsY);
                        if(totalPixelsY<0){
                            Log.i("David", "scroll down");
                            totalPixelsY=event.getRawY() - startY;
                            Log.i("David", "We moved "+totalPixelsY+" pixels");
                        }else{
                            Log.i("David", "scroll up");
                            Log.i("David", "We moved "+totalPixelsY+" pixels");
                        }
                    }else{
                        movX=1;
                        Log.i("David", "Swipe right action up");
                        float totalPixelsX=Math.abs(startX-endX);
                        float totalPixelsY=event.getRawY() - startY;
                        Log.i("David", "totalPixelsx: "+totalPixelsX);
                        Log.i("David", "totalPixelsY: "+totalPixelsY);
                        if(totalPixelsY<0){
                            Log.i("David", "scroll down");
                            totalPixelsY=event.getRawY() - startY;
                            Log.i("David", "We moved "+totalPixelsY+" pixels");
                        }else{
                            Log.i("David", "scroll up");
                            Log.i("David", "We moved "+totalPixelsY+" pixels");
                        }
                    }
                    Log.i("David", "haveFlipped false");
                    haveFlipped=false;
                    break;

                case MotionEvent.ACTION_CANCEL:
                    endX = event.getX();
                    endY = event.getRawY();
                    if(startX-endX < 0){
                        movX=0;
                        Log.i("David", "Swipe left action cancel");
                        float totalPixelsX=Math.abs(startX-endX);
                        float totalPixelsY=event.getRawY() - startY;
                        Log.i("David", "totalPixelsx: "+totalPixelsX);
                        Log.i("David", "totalPixelsY: "+totalPixelsY);
                        if(totalPixelsY<0){
                            Log.i("David", "scroll down");
                            totalPixelsY=event.getRawY() - startY;
                            Log.i("David", "We moved "+totalPixelsY+" pixels");
                        }else{
                            Log.i("David", "scroll up");
                            Log.i("David", "We moved "+totalPixelsY+" pixels");
                        }
                    }else{
                        movX=1;
                        Log.i("David", "Swipe right action cancel");
                        float totalPixelsX=Math.abs(startX-endX);
                        float totalPixelsY=event.getRawY() - startY;
                        Log.i("David", "totalPixelsx: "+totalPixelsX);
                        Log.i("David", "totalPixelsY: "+totalPixelsY);
                        if(totalPixelsY<0){
                            Log.i("David", "scroll down");
                            totalPixelsY=event.getRawY() - startY;
                            Log.i("David", "We moved "+totalPixelsY+" pixels");
                        }else{
                            Log.i("David", "scroll up");
                        }
                    }
                    Log.i("David", "haveFlipped false");
                    haveFlipped=false;
                    break;

            }
            return true;
        }
    };

我的问题是,在我的手指从电话屏幕抬起之前调用action_cancel或action_up,因此Y轴的移动量总是小于我手指的实际移动量。

我计划实现的是,当X> y的量时,使用ViewFlipper,当X的量<y时,让ScrollView完成他的工作。

我不知道我的方法是否正确。谁能告诉我为什么在举起手指之前调用action_up和action_cancel?以及如何实现我的目标?

谢谢。

android ontouchlistener android-viewflipper
2个回答
0
投票

如果ViewFlipper在其onTouch()方法中返回TRUE,那么唯一的方法是创建一个扩展RelativeLayout的新类并实现其onInterceptTouchEvent()方法。

public class MyRelativeLayout extends RelativeLayout {
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return super.onInterceptTouchEvent(ev);
    }
}

然后使用此MyRelativeLayout作为XML布局文件的根目录。


0
投票

我设法通过从MotionEvent.ACTION_CANCEL和MotionEvent.ACTION_UP中删除这些行来实现它:

endX = event.getX();
endY = event.getRawY();

然后,在交换机上添加:

case MotionEvent.ACTION_MOVE:
endX = event.getX();
endY = event.getRawY();
break;

这样,ScrollView和ViewFlipper都可以正常运行。

© www.soinside.com 2019 - 2024. All rights reserved.