[英]Navigation Drawer and FragmentActivity
I'm relatively new to android development. 我是android开发的新手。 Basically, I got a Navigation Drawer.
基本上,我有一个导航抽屉。 One of the menu will need to open FragmentActivity with viewpager.
菜单之一将需要使用viewpager打开FragmentActivity。
here the displayview function 这里的displayview功能
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
LAST_DISPLAY_POSITION = position;
Fragment fragment = null;
switch (position) {
case 0:
Intent in = new Intent(getApplicationContext(), TestFragmentActivity.class);
startActivity(in);
break;
case 1:
fragment = new MyAccountFragment();
break;
case 2:
fragment = new TestFragment();
break;
case 3:
fragment = new TestFragment();
break;
case 4:
fragment = new TestFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
as you can see at Case 0, I just calling startactivity to open the view. 如案例0所示,我只是调用startactivity打开视图。 How can I display the FragmentActivity with the Navigation Drawer?
如何使用导航抽屉显示FragmentActivity?
Any help would be greatly appreciated. 任何帮助将不胜感激。
EDIT 编辑
my activity_main.xml 我的activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<!-- Listview to display slider menu -->
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/list_background"
android:choiceMode="singleChoice"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />
</android.support.v4.widget.DrawerLayout>
Do one thing, right now you might have written code for NavigationDrawer
in your single activity say DisplayFragmentsActivity
and you are going to another activity TestFragmentActivity
which will surely make you write code for NavigationDrawer
in that activity too plus you will need to manage all other things for selection of menu if you are handling. 做一两件事,现在你可能已经编写了代码
NavigationDrawer
在单一的活动说DisplayFragmentsActivity
和你要去另一个活动TestFragmentActivity
这必将让你写代码NavigationDrawer
该活动也加上,你需要管理所有其他的事情选择菜单(如果要处理)。
So, I suggest move your code of NavigationDrawer
from DisplayFragmentsActivity
to a new MasterFragmentActivity
and extend that activity to your DisplayFragmentsActivity
and TestFragmentActivity
. 因此,我建议将
NavigationDrawer
的代码从DisplayFragmentsActivity
移到新的MasterFragmentActivity
然后将该活动扩展到DisplayFragmentsActivity
和TestFragmentActivity
。 So now you will have the same drawer in your both the activities. 因此,现在您将在两个活动中使用相同的抽屉。
This will solve the problem. 这样可以解决问题。
Another suggestion you might look to avoid creating another activity, as you are using fragments try to use single activity only. 您可能希望避免创建另一个活动的另一个建议,因为您正在使用片段,请尝试仅使用单个活动。 try to find some replacement for
TestFragmentActivity
. 尝试找到一些替代品
TestFragmentActivity
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.