OnTouchListener - 在 ViewPager 中单击和滑动之间的问题

OnTouchListener - Issue between click and swipe inside a ViewPager

我实现了一个没有片段的 ViewPager(只是视图)。每个页面都包含一个元素列表。通过滑动动作,我可以访问 ViewPager 的下一页,通过点击事件,我可以显示有关所选元素的信息。

为了区分点击和滑动动作,我在列表的每个元素上实现了一个 TouchListener :

public boolean onTouch(View v, MotionEvent event) {

    int move = event.getAction();

    switch(move){
        case MotionEvent.ACTION_DOWN:
            lastX = event.getX();
            lastY = event.getY();
            //mViewPager.dispatchTouchEvent(event);
            return true;
        case MotionEvent.ACTION_UP:
            float diffX = Math.abs(lastX-event.getX());
            float diffY = Math.abs(lastY-event.getY());
            if(diffX < incertitude && diffY < incertitude){
                Bundle b = new Bundle();
                b.putString(HOME_TITLE,n.getTitle());
                b.putString(HOME_DESCR,n.getArticle());

                FragmentManager fm = me().getFragmentManager();
                HomeDialogFragment mDialogFragment = new HomeDialogFragment();
                mDialogFragment.setArguments(b);
                mDialogFragment.show(fm,"home_dialog_fragment");
                return true;
            }
        default:
            return false;
    }
}

这个解决方案并不完美,我也不知道为什么。 列表的每个元素基本上都是由几个TextView组成的。点击动作效果很好,但我对滑动动作有一些问题。

当我尝试在元素上开始滑动时,它起作用了(我可以访问下一页)。另一方面,当我在其中一个 TextView 上开始滑动时,它根本不起作用:

(感谢 Stack 不允许我使用 post 有用的图片...) http://www.hostingpics.net/viewer.php?id=829040170.png

当我从 "orange background" 区域之一(textView 之一)开始滑动时,没有任何反应。否则,没关系。我不明白为什么...

列表元素的XML文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/home_screen"
    android:layout_width="match_parent"
    android:layout_height="?android:listPreferredItemHeightLarge"
    android:divider="?android:dividerVertical"
    android:dividerPadding="8dp"
    android:orientation="horizontal"
    android:showDividers="middle">

    <TextView
        android:id="@+id/home_title"
        style="?android:textAppearanceLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:gravity="center"
        android:layout_marginLeft="100dp"
        android:textColor="@color/news_title"
        android:layout_alignParentRight="false"
        android:layout_alignParentTop="false"
        android:textStyle="bold"
        android:textSize="30dp"
        android:layout_marginTop="10dp"
        android:singleLine="true"
        android:ellipsize="end"
        android:layout_marginRight="30dp"
        android:background="@android:color/holo_orange_light"
        android:text="Titre principal"/>

    <TextView
        android:id="@+id/home_description"
        style="?android:textAppearanceLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:gravity="center"
        android:layout_marginLeft="100dp"
        android:textColor="@color/news_subtitle"
        android:layout_alignParentRight="false"
        android:layout_alignParentBottom="false"
        android:layout_marginTop="40dp"
        android:singleLine="true"
        android:ellipsize="end"
        android:layout_marginRight="30dp"
        android:text="Description sommaire"
        android:background="@android:color/holo_orange_light"/>

        ...

</RelativeLayout>

感谢您的帮助!

刚刚找到答案:这是一个 Android 使用时的问题:

  • 一个ViewPager
  • Gravity="center"
  • SingleLine="true"

More Info