我有一个带有两个不同抽屉的主活动: - 左侧的导航栏 - 右侧的通知
布局 main.xml 如下:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.toto.MainActivity">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"/>
<fragment android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.test.toto.NavigationDrawer" />
<fragment android:id="@+id/notification_drawer"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="end"
android:name="com.test.toto.NotificationDrawer" />
</android.support.v4.widget.DrawerLayout>
在我的主要活动 MainActivity.java 中,我执行以下操作:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mNavigationDrawer = (NavigationDrawer)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mNotificationDrawer = (NotificationDrawer)
getFragmentManager().findFragmentById(R.id.notification_drawer);
// Set up the drawers
mNavigationDrawer.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
mNotificationDrawer.setUp(
R.id.notification_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
在两个抽屉片段中都有setUp方法(例如 NotificationDrawer.java ):
public void setUp(int fragmentId, DrawerLayout drawerLayout) {
mFragmentContainerView = getActivity().findViewById(fragmentId);
mDrawerLayout = drawerLayout;
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.END);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the navigation drawer and the action bar app icon.
mDrawerToggle = new ActionBarDrawerToggle(
getActivity(), /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.navigation_drawer_open, /* "open drawer" description for accessibility */
R.string.navigation_drawer_close /* "close drawer" description for accessibility */
) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (!isAdded()) {
return;
}
getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (!isAdded()) {
return;
}
}
};
// Defer code dependent on restoration of previous instance state.
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});
// HERE IS THE PROBLEM
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
我的问题是mDrawerLayout在两个设置(R.id.drawer_layout)中都是相同的,所以当我第二次调用setUp时,setDrawerListener方法会被新的Listener覆盖(在我的例子中是导航一个)取代我的通知一。)
因此,导航抽屉不会调用导航抽屉与操作栏( onDrawerClosed 和 onDrawerOpened )之间进行交互的方法。
如何为两个抽屉调用这些方法? 当打开一个或另一个抽屉时,我需要在两种情况下都做一些事情。
答案 0 :(得分:0)
你的听众必须处理两个抽屉。在OnDrawerOpened和OnDrawerClosed中,您必须检查参数中的哪个抽屉并使用正确的代码。