DavidL
DavidL

Reputation: 1150

OnTouchListener - Issue between click and swipe inside a ViewPager

I have a ViewPager implemented without fragment (just view). Each page contains a list of elements. With a swipe movement, I can access the next page of the ViewPager, and with a click event, I can display information about the chosen element.

In order to distinguish click and swipe actions, I have implemented a TouchListener on each element of the list :

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;
    }
}

This solution don't work perfectly, and I don't no why. Each element of the list is basically composed of several TextView. The click action works well, but I have some issue with the swipe movement.

When I try to swipe starting on the element, it works (I can access the next page). On the other hand, it doesn't work at all when I swipe starting on one of the TextView:

(Thanks Stack for not allowing me to post helpful image...) http://www.hostingpics.net/viewer.php?id=829040170.png

When I initiate the swipe movement from one of the "orange background" area (one of the textView), nothing happens. Otherwise, it's fine. And I don't understand why...

The XML file of a list element:

<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>

Thanks for your help !

Upvotes: 2

Views: 805

Answers (1)

DavidL
DavidL

Reputation: 1150

Just found the answer: it's an Android issue when using:

  • a ViewPager
  • Gravity="center"
  • SingleLine="true"

More Info

Upvotes: 2

Related Questions