Android 导航抽屉默认片段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22296531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Navigation drawer default fragment
提问by user3383415
I am novice developer and I′m integrating Navigation drawer in my app with android-support v7 and I have one question. When I start the app the main layout is this:
我是新手开发人员,我正在将 Navigation drawer 与 android-support v7 集成到我的应用程序中,我有一个问题。当我启动应用程序时,主要布局是这样的:
<?xml version="1.0" encoding="utf-8"?>
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
and this is my main activity:
这是我的主要活动:
drawerList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragment2();
break;
case 2:
fragment = new Fragment3();
break;
case 3:
fragment = new Fragment4();
break;
}
FragmentManager fragmentManager =
getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();
drawerList.setItemChecked(position, true);
tituloSeccion = opcionesMenu[position];
getSupportActionBar().setTitle(tituloSeccion);
drawerLayout.closeDrawer(drawerList);
}
});
How can I set default fragment like main layout of the app? Thank you
如何设置默认片段,如应用程序的主布局?谢谢
回答by super-qua
If it is ok for you to load the default fragment every time your activity is created, you can put a FragmentTransactionin onCreate()
如果这是确定为您加载默认片段每次创建您的活动时间,你可以把FragmentTransaction在onCreate()
Looks something like this:
看起来像这样:
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.content_frame, new Fragment1());
tx.commit();
}
If you want a more sophisticated way of doing this (for example switching to a different fragment when you go back to the main activity), you can use an Intentwith extras determining the fragment in onCreate(), where you just put your default fragment in the defaultValueupon loading the extra:
如果您想要一种更复杂的方式来执行此操作(例如,当您返回主活动时切换到不同的片段),您可以使用Intentwith extras 来确定 中的片段onCreate(),您只需defaultValue在加载时将默认片段放入额外的:
int position = getIntent().getIntExtra("position", 1);
switch(position){
...
}
回答by Jemshit Iskenderov
Put this at the end of onCreate() in Main Activity.
把它放在主活动中 onCreate() 的末尾。
if (savedInstanceState == null) {
selectItem(0);
}
回答by sorianiv
You can't do it in the XML, if that's what you mean. You do it in the onCreate of your activity. For example:
如果这就是您的意思,您不能在 XML 中执行此操作。您在活动的 onCreate 中执行此操作。例如:
if (getFragmentManager().findFragmentById(R.id.content_frame) == null) {
selectItem(0);
}
where selectItem is the method you use to select fragments in your drawer, as in google's example https://developer.android.com/training/implementing-navigation/nav-drawer.html, and 0 is the position of the default fragment.
其中 selectItem 是您用来在抽屉中选择片段的方法,如谷歌的示例https://developer.android.com/training/implementing-navigation/nav-drawer.html,0 是默认片段的位置。
回答by Haziq Naeem
Try this in oncreate:
在 oncreate 中试试这个:
Fragment fragment = null;
fragment = new HomeFragment();
//HomeFragment= fragment class to launch that
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.screen_area, fragment);
fragmentTransaction.commit();
回答by Sai Vutukuri
Its Helps, it is working
它的帮助,它正在工作
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
FragmentManager fragmentManager= getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame
,new YourClass())
.commit();
}
回答by Sumant Us
Add a default case in your switch code.
在您的开关代码中添加默认情况。
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragment2();
break;
case 2:
fragment = new Fragment3();
break;
case 3:
fragment = new Fragment4();
break;
default:
fragment = new Fragment1();
break;
}

