从片段中禁用导航抽屉 [英] Disabling navigation drawer from fragment

查看:22
本文介绍了从片段中禁用导航抽屉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有导航抽屉和 4 个导航项(片段)的应用.在其中一个片段中,我设置了一个带有视图寻呼机的选项卡布局(另外 3 个片段).

I have an app with a navigation drawer and 4 navigation items (Fragments). In one of the Fragments, I have a tab layout set up with a view pager (3 more Fragments).

从这些内部片段之一,我想动态禁用/启用导航抽屉.基本上,在按下按钮时,我想限制对导航抽屉的访问(并在再次按下时重新启用).

From one of these inner fragments, I want to disable/enable the navigation drawer dynamically. Basically, on a button press, I want to restrict access to the navigation drawer (and the re-enable on pressing it again).

我该怎么做?

我尝试从这个内部片段访问父活动的 DrawerLayout.但我看不到启用/禁用导航抽屉的方法.

I tried accessing the DrawerLayout of the parent activity from this inner fragment. But I see no methods to enable/disable the navigation drawer.

我将抽屉添加到主 Activity 的方式:

The way I've added the drawer to my main Activity:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);

(当然,我在 onPostCreate 方法中添加了 toggle.syncState().

(and of course I've added toggle.syncState() from within the onPostCreate method.

推荐答案

一个干净的方法是创建一个 Activity 实现的 interface,通过它Fragment 可以调用处理抽屉锁定和切换按钮状态的 Activity 本地方法.例如:

A clean way to do this is to create an interface that the Activity implements, through which the Fragment can call a method local to the Activity that handles the drawer lock and toggle button states. For example:

public interface DrawerLocker {
    public void setDrawerEnabled(boolean enabled);
}

Activityinterface 方法中,我们简单地计算 DrawerLayout#setDrawerLockMode() 调用的锁定模式常量,然后调用setDrawerIndicatorEnabled()ActionBarDrawerToggle 上.

In the Activity's interface method, we simply figure the lock mode constant for the DrawerLayout#setDrawerLockMode() call, and call setDrawerIndicatorEnabled() on the ActionBarDrawerToggle.

public class MainActivity extends Activity implements DrawerLocker {

    public void setDrawerEnabled(boolean enabled) {
        int lockMode = enabled ? DrawerLayout.LOCK_MODE_UNLOCKED :
                                 DrawerLayout.LOCK_MODE_LOCKED_CLOSED;
        drawer.setDrawerLockMode(lockMode);
        toggle.setDrawerIndicatorEnabled(enabled);
    }

    ...
}

Fragment中,我们只需要将宿主Activity强制转换为interface,并调用setDrawerEnabled() 方法相应地.例如,锁上抽屉:

In the Fragment, we merely need to cast the hosting Activity to the interface, and call the setDrawerEnabled() method accordingly. For example, to lock the drawer shut:

((DrawerLocker) getActivity()).setDrawerEnabled(false);

<小时>

注意:由于 v7 appcompat 支持库的版本 23.2.0,ActionBarDrawerToggle 尊重 DrawerLayout 的锁定模式,如果它是不会切换抽屉状态锁定.这意味着使用 setDrawerIndicatorEnabled() 并不是绝对必要的,尽管为了向用户提供禁用切换的视觉指示可能仍然需要这样做.


NB: Since version 23.2.0 of the v7 appcompat support library, ActionBarDrawerToggle respects the DrawerLayout's lock mode, and will not toggle the drawer state if it is locked. This means that it is not strictly necessary to use setDrawerIndicatorEnabled(), though it might be desirable to still do so in order to provide the user a visual indication that the toggle is disabled.

这篇关于从片段中禁用导航抽屉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆