Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make fragment clickable when navigation drawer is opened

My problem is as follows: I lock the navigation drawer menu setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN) in the landscape mode of the tablet, but I need the fragment from the right to be active, so I can click it with navigation always opened. But I dont know how to do it. Please help.

Screenshot

like image 627
Max Zavernutiy Avatar asked Feb 12 '15 19:02

Max Zavernutiy


2 Answers

There are a few things you need to do:

  1. Disable the layout fading by setting a transparent color:

    drawer.setScrimColor(Color.TRANSPARENT);
    
  2. Lock the drawer

    drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
    
  3. Create a custom drawer class which allows clicking through when in locked 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);
            }
        }
    
    }
    
  4. 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>
    
like image 157
Simas Avatar answered Nov 15 '22 11:11

Simas


This is a tricky one. When the drawer is open, it intercepts your touch events which trigger the close of the drawer. In order to prevent that, you need to subclass your 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 a right drawer. I'm sure you can adapt this for your left drawer. You might also want to disable the shadow:

mDrawerLayout.setScrimColor(Color.TRANSPARENT);
like image 20
Quanturium Avatar answered Nov 15 '22 13:11

Quanturium