Make the fragment clickable when opening the navigation box

My problem is this: I am blocking the setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN) navigation drawer menu setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN) in tablet landscape mode, but I need the fragment on the right to be active, so I can click it when the navigation is always open. But I do not know how to do this. Please help.

Screen shot

+6
source share
2 answers

There are a few things you need to do:

  • Turn off layout fading by setting a transparent color:

     drawer.setScrimColor(Color.TRANSPARENT); 
  • Lock box

     drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN); 
  • Create your own box class that allows you to switch to lock mode:

     public class CustomDrawer extends DrawerLayout { public CustomDrawer(Context context) { super(context); } public CustomDrawer(Context context, AttributeSet attrs) { super(context, attrs); } public CustomDrawer(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { View drawer = getChildAt(1); if (getDrawerLockMode(drawer) == LOCK_MODE_LOCKED_OPEN && ev.getRawX() > drawer.getWidth()) { return false; } else { return super.onInterceptTouchEvent(ev); } } } 
  • Use this class in xml:

     <com.example.myapplication.CustomDrawer xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!-- The main content view --> </FrameLayout> <ListView android:layout_width="100dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#111"/> </com.example.myapplication.CustomDrawer> 
+7
source

It's complicated. When the drawer is open, it captures your touch events, which cause the drawer to close. To avoid this, you need to subclass DrawerLayout and override the onInterceptTouchEvent method:

 public class CustomDrawerLayout extends DrawerLayout { private View rightView; private int mTouchSlop; public CustomDrawerLayout (Context context) { this(context, null); } public CustomDrawerLayout (Context context, AttributeSet attrs) { this(context, attrs, 0); } public CustomDrawerLayout (Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(ViewConfiguration.get(context)); } public void setRightView (View v) { this.rightView = v; } @Override public boolean onInterceptTouchEvent (MotionEvent ev) { boolean result = super.onInterceptTouchEvent(ev); if (rightView != null && isDrawerOpen(rightView)) { DrawerLayout.LayoutParams layoutParams = (DrawerLayout.LayoutParams) rightView.getLayoutParams(); if (layoutParams.gravity == Gravity.END) { // This is true when the position.x of the event is happening on the left of the drawer (with gravity END) if (ev.getX() < rightView.getX() && ev.getX() > mTouchSlop) { result = false; } } } return result; } } 

This is my code working with the correct box. I am sure you can adapt this for your left drawer. You can also turn off the shadow:

 mDrawerLayout.setScrimColor(Color.TRANSPARENT); 
+2
source

Source: https://habr.com/ru/post/982398/


All Articles