【发布时间】:2014-03-20 00:09:42
【问题描述】:
我有一个带有两个不同抽屉的主 Activity: - 左侧的导航 - 右侧的通知之一
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 方法被新的侦听器覆盖(在我的情况下是导航之一被替换为我的通知之一)。
因此,导航抽屉和操作栏之间的交互方法(onDrawerClosed 和 onDrawerOpened)不会为导航抽屉调用。
如何让两个抽屉都调用这些方法? 当一个或另一个抽屉打开时,我需要在这两种情况下做一些事情。
【问题讨论】:
标签: java android android-fragments navigation-drawer slidingdrawer