Show/Hide DrawerLayout depends on current fragment

岁酱吖の 提交于 2019-12-07 03:41:33

问题


I have an Activity class that have a DrawerLayout. That layout shows a list in Drawer and let user to switch between fragments. In these Fragments, there are some URLs and when user clicsk on that, a WebviewFragment would be shown. However, I don't want to show the DrawerLayout in the WebViewFragment. Instead, I would prefer user would being redirected to previous Fragment.

Is there any way for me to show/hide the DrawerLayout depends on what the current Fragment is? I try to call mDrawerLayout.setVisibility(View.GONE), but it seems that it is not complete. At least the ActionBar icon is still the drawer icon.


回答1:


You can use this method to lock or unlock the drawer: DrawerLayout.setDrawerLockMode(...). (There are also two other versions of this method to specify a lock mode for specific drawers.) To lock, use DrawerLayout.LOCK_MODE_LOCKED_CLOSED; to unlock, use DrawerLayout.LOCK_MODE_UNLOCKED.

If you are using the ActionBarDrawerToggle, you need to add some extra code to prevent the drawer from opening when they click the ActionBarDrawerToggle if you've locked the drawer.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // check lock mode before passing to ActionBarDrawerToggle
    // I assume your drawer is on the left; if not, use Gravity.RIGHT
    int lockMode = mDrawer.getDrawerLockMode(Gravity.LEFT);
    if (lockMode == DrawerLayout.LOCK_MODE_UNLOCKED &&
            mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // Handle your other action bar items...

    return super.onOptionsItemSelected(item);
}



回答2:


Why not open a new Activity that has only a WebView as its content instead? Then the user will have to press the back button to go back to the Activity with the DrawerLayout, and then there will be no problems.

Alternatively, you don't have to have such an activity yourself, you can let Android find a browser for them to open instead using this code:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("HTTP_URL_GOES_HERE"));
startActivity(intent);


来源:https://stackoverflow.com/questions/18158542/show-hide-drawerlayout-depends-on-current-fragment

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