I have a ViewPager with 3 fragments in it. I have extended ViewPager and disabled its swiping abilities for reasons specific to my app. So, for navigation between these fragments, I've added a TabLayout to to my activity.
In my second fragment (the one "in the middle") I have a DrawerLayout on the left edge.
Everytime I make a swiping motion from left to right in that fragment, the DawerLayout is shown, even if I swipe in the middle of the screen. It should only happen if I swiped close to the left edge of the screen.
I've been having a hard time understanding touch and motion events handling in Android... Is there a way to keep my ViewPager (swiping disabled) and restrict the touch events DrawerLayout 'listens to' to the edge of the screen (as its supposed to)?
If I do not modify the ViewPager (that is, if I keep swiping enabled), it also does not work because swiping from the left edge will begin to open the DrawerLayout, but at the same time, will begin the ViewPager's transition.
I've recreated the issue in a small app.
This is MyViewPager:
public class MyViewPager extends ViewPager {
public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return false;
}
}
This is MainActivity:
public class MainActivity extends AppCompatActivity {
Fragment Fragment1;
Fragment Fragment2;
Fragment Fragment3;
MyViewPager viewPager;
TabLayout tabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment1 = new Fragment1();
Fragment2 = new Fragment2();
Fragment3 = new Fragment3();
viewPager = findViewById(R.id.ViewPager);
tabLayout = findViewById(R.id.TabLayout);
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
if (Fragment1 == null) {
Fragment1 = new Fragment1();
}
return Fragment1;
case 1:
if (Fragment2 == null) {
Fragment2 = new Fragment2();
}
return Fragment2;
case 2:
if (Fragment3 == null) {
Fragment3 = new Fragment3();
}
return Fragment3;
default:
return null;
}
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "frag1";
case 1:
return "frag2";
case 2:
return "frag3";
default:
return "0";
}
}
});
viewPager.setOffscreenPageLimit(2);
viewPager.setCurrentItem(0);
tabLayout.setupWithViewPager(viewPager);
}
}
ActivityMain' layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="@+id/main_frame"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4dea21">
<com.example.android.navbarviewpager.MyViewPager
android:id="@+id/ViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#ea3ee2"/>
<android.support.design.widget.TabLayout
android:id="@+id/TabLayout"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_gravity="left|bottom"
app:tabMode="scrollable">
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="frag1"/>
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="frag2"/>
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="frag3"/>
</android.support.design.widget.TabLayout>
</FrameLayout>
Fragment1:
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
}
}
Fragment2:
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2, container, false);
}
}
Fragment3:
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment3, container, false);
}
}
Fragment TWO's layout (the one with the DrawerLayout):
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/frag_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#a2a2ee"
android:padding="0dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="fragmento DOIS"/>
</FrameLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:background="#b9b9b9"
android:clickable="true"
android:orientation="vertical"
android:paddingBottom="20dp"
android:paddingTop="20dp">
<TextView
android:id="@+id/notified_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TTTTT"/>
<TextView
android:id="@+id/overdue_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="XXXXX"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
And the other fragments' layouts:
Fragment ONE
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="@+id/frag_1"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eea2a2"
android:padding="0dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="fragmento UM"
android:gravity="center"/>
</FrameLayout>
Fragment THREE
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="@+id/frag_1"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#abeea2"
android:padding="0dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="fragmento TRÊS"
android:gravity="center"/>
</FrameLayout>