首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与onTouchListener抗争

与onTouchListener抗争
EN

Stack Overflow用户
提问于 2019-03-07 19:57:23
回答 2查看 38关注 0票数 1

我有一个ViewFlipper,我想在它上面设置一个touchListener。它在ScrollView中(在ViewFlipper下面有更多的UI元素),我希望如果用户进行垂直移动,scrollview就会起作用,当用户进行水平移动时,ViewFlipper就会起作用。(组件是在ViewFlipper中动态添加的)。

为了清楚起见,以下是布局:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
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吗?如何实现我的目标呢?

谢谢。

EN

回答 2

Stack Overflow用户

发布于 2019-03-07 20:48:53

如果ViewFlipper的onTouch()方法返回TRUE,那么惟一的方法就是创建一个新的类来扩展RelativeLayout并实现它的onInterceptTouchEvent()方法。

代码语言:javascript
复制
public class MyRelativeLayout extends RelativeLayout {
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return super.onInterceptTouchEvent(ev);
    }
}

然后使用这个MyRelativeLayout作为您的XML布局文件的根。

票数 0
EN

Stack Overflow用户

发布于 2019-03-07 21:06:27

通过从MotionEvent.ACTION_CANCEL和MotionEvent.ACTION_UP中删除以下代码行,我设法使其正常工作:

代码语言:javascript
复制
endX = event.getX();
endY = event.getRawY();

然后,在交换机上添加以下内容:

代码语言:javascript
复制
case MotionEvent.ACTION_MOVE:
endX = event.getX();
endY = event.getRawY();
break;

这样,ScrollView和ViewFlipper都能按其应有的方式运行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55043225

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档