Make fragment clickable when navigation drawer is opened

霸气de小男生 提交于 2019-12-04 21:45:37

问题


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.


回答1:


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>
    



回答2:


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


来源:https://stackoverflow.com/questions/28486385/make-fragment-clickable-when-navigation-drawer-is-opened

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!