我的应用程序在开始时打开NavigationDrawer。 当用户点击其中的任何项目时,正在替换片段。
对于导航抽屉中的每个项目,我都有一个单独的片段。 但是当用户没有选择NavigationDrawer和CLOSES中的任何选项时, 正在显示主要的活动布局。
当用户关闭侧边菜单时,我希望自己的一个片段被启动。 (导航抽屉) 但是当我试图改变
时,我得到了一个NPEsetContentView(R.layout.activity_main);
带
setContentView(R.layout.my_fragment);
(因为它在activity_main中定义,因此无法找到navigation_drawer)
我不想把我的NavigationDrawer放在我的一个片段中,我也不喜欢将我的片段元素复制到我的main.xml(它将是一个混乱的设计:()。 我应该做些什么? 我怎么能在NavigationDrawer下打开我想要的片段?
这是我在MainActivity中的onCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (FragmentNavigationDrawer) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
mTitle = getTitle();
}
这是我的activity_main.xml
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<!--
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:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead.
-->
<!--
The drawer is given a fixed width in dp and extends the full height of
the container.
-->
<fragment
android:id="@+id/navigation_drawer"
android:name="com.khoonat.khoonat.FragmentNavigationDrawer"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="@layout/fragment_navigation_drawer" />
这是一个糟糕的解决方案吗? 请注意,我是初学者:)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container, FragmentSearchPropertyListing.newInstance(0)).commit();
mNavigationDrawerFragment = (FragmentNavigationDrawer) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
mTitle = getTitle();
}
红色箭头表示导航抽屉下的含义。