问题描述
我有一个带有导航抽屉和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.
我将抽屉添加到主活动中的方式:
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
可以通过该interface
调用本地的方法. 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);
}
在Activity
的interface
方法中,我们只需计算DrawerLayout#setDrawerLockMode()
调用的锁定模式常量,然后在ActionBarDrawerToggle
上调用setDrawerIndicatorEnabled()
.
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);
NB:自v7 appcompat支持库的23.2.0版本以来,ActionBarDrawerToggle
遵守DrawerLayout
的锁定模式,并且如果锁定了抽屉,则不会切换抽屉状态.这意味着并不一定要使用setDrawerIndicatorEnabled()
,尽管可能仍希望使用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.
这篇关于从片段禁用导航抽屉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!