0

I have an AppCompatActivity with a layout that contains a single FrameLayout. In that FrameLayout, I place a Fragment that contains a more complex layout :

<?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.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include layout="@layout/top_toolbar" />

        <android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <include layout="@layout/bottom_toolbar" />

            <com.passionusnetworks.lype.plugins.SlidingTabLayout
                android:id="@+id/sliding_tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/abc_action_bar_default_height_material"
                android:background="#00000000"
                android:elevation="2dp" />

            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#FFFFFF" />

            <include layout="@layout/navigation_drawer" />

        </android.support.v4.widget.DrawerLayout>

    </android.support.design.widget.CoordinatorLayout>

</RelativeLayout>

And then I place 2 Fragment in the ViewPager. With my SlidingTabLayout, I can switch between my 2 fragments by pressing on those tabs. In those fragments, there is a RecyclerView. My problem is that no touch event seems to reach my fragments (the RecyclerViews cannot scroll and I cannot swipe between the fragments of the ViewPager. I've tried to put some OnTouchListener on all levels but I could never get a single MotionEvent.

I don't know what to do to find why I can't get any touch events in my fragments. Does someone have a suggestion?

Raphael Royer-Rivard
  • 2,252
  • 1
  • 30
  • 53
  • Try this [link](http://stackoverflow.com/questions/8122460/viewpager-intercepts-all-x-axis-ontouch-events-how-to-disable). It should work – Praween Kumar Mishra Jan 12 '16 at 13:09
  • I already tried the `OnTouchListener` like I said and its no use, I get no `MotionEvent` when I tap or swipe. I also tried the `requestDisallowInterceptTouchEvent(true);` without success but I am not sure if I was putting it at the right place. – Raphael Royer-Rivard Jan 12 '16 at 14:28

2 Answers2

0
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
// Inflate the layout containing a title and body text.
ViewGroup rootView = (ViewGroup) inflater
            .inflate(R.layout.fragment_slide, container, false);
LinearLayout layout =          (LinearLayout)rootView.findViewById(R.id.layout)//      get your root  layout
  layout.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.v(null, "TOUCH EVENT"); // handle your fragment number here
        return false;
    }
});
  return rootView;
}
Akshay Panchal
  • 695
  • 5
  • 15
  • On which layout should I try to set the `OnTouchListener`? On the `FrameLayout` of my `AppCompatActivity`, on the `RelativeLayout` of my `Fragment` or on the layout of a fragment contained in the `ViewPager`? – Raphael Royer-Rivard Jan 12 '16 at 14:17
0

To have the right behavior, I ended up putting the CoordinatorLayout inside the DrawerLayout, changing the SlidingTabLayout to a TabLayout (and moved it inside the AppBarLayout of the @layout/top_toolbar). I also added app:layout_behavior="@string/appbar_scrolling_view_behavior" to the ViewPager.

I based my code on the great Cheesesquare demo.

Raphael Royer-Rivard
  • 2,252
  • 1
  • 30
  • 53