Navigation drawer - Disable click through on items behind the drawer

风格不统一 提交于 2019-12-03 04:09:44
Gennadiy Ryabkin

Set android:clickable="true" tag on sliding pane layout.

The problem seem not because of click focus,

Visit https://developer.android.com/training/implementing-navigation/nav-drawer.html#DrawerLayout

The main content view (the FrameLayout above) must be the first child in the DrawerLayout because the XML order implies z-ordering and the drawer must be on top of the content.

In my fragment drawer, I set TouchListener to return True. It worked for me

        mFragmentContainerView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    });

I solved it in a different way.

Here is my code for setting up the Drawer:

/**
 * Setup Navigation Drawer
 */
private void setDrawer() {
    NavigationDrawerFragment mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
    mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
}

the setup method is inside my NavigationDrawerFragment, here is my code for it:

/**
 * Users of this fragment must call this method to set up the navigation drawer interactions.
 *
 * @param fragmentId   The android:id of this fragment in its activity's layout.
 * @param drawerLayout The DrawerLayout containing this fragment's UI.
 * @param toolbar      The Toolbar of the activity.
 */
public void setup(int fragmentId, DrawerLayout drawerLayout, Toolbar toolbar) {
    View mFragmentContainerView = (View) getActivity().findViewById(fragmentId).getParent();
    DrawerLayout mDrawerLayout = drawerLayout;

    //noinspection deprecation
    mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));

    ActionBarDrawerToggle mActionBarDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, toolbar, "Drawer opened", "Drawer closed") {
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            if (!isAdded()) return;

            // Solution:
            // Disable click event on views below Navigation Drawer
            mFragmentContainerView.setClickable(false);
            getActivity().invalidateOptionsMenu();
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            if (!isAdded()) return;

            // Solution:
            // Enable click event on views below Navigation Drawer
            mFragmentContainerView.setClickable(true);
            getActivity().invalidateOptionsMenu();
        }
    };

    // Defer code dependent on restoration of previous instance state.
    mDrawerLayout.post(new Runnable() {
        @Override
        public void run() {
            mActionBarDrawerToggle.syncState();
        }
    });

    //noinspection deprecation
    mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
}

That's it

Well, I found one solution for this issue. When the drawer is being opened you can bring the nav bar to the front by calling the bringToFront()-method on the layout you use. This makes sure the navigation drawer stays on top of any underlying content until a new item has been selected.

I.e:

@Override
public void onDrawerOpened(View drawerView)
   {
   activity.getActionBar().setTitle("Select content");
   activity.invalidateOptionsMenu();
   drawerLayout.bringToFront();
   }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!