Android 如何使用导航抽屉管理基本活动
在我的导航抽屉中,我不想使用activity以及如何正确使用fragment。Android 如何使用导航抽屉管理基本活动,android,performance,android-layout,android-activity,fragment,Android,Performance,Android Layout,Android Activity,Fragment,在我的导航抽屉中,我不想使用activity以及如何正确使用fragment。在MainActivityClass上导入FragmentDrawer.FragmentDrawerListener。而不是为您希望在应用程序中替换的任意多个片段编写下面的代码。 Impment FragmentDrawer.FragmentDrawerListener on your MainActivityClass. than write belowed code for as many fragment you
在MainActivityClass上导入FragmentDrawer.FragmentDrawerListener。而不是为您希望在应用程序中替换的任意多个片段编写下面的代码。
Impment FragmentDrawer.FragmentDrawerListener on your MainActivityClass. than write belowed code for as many fragment you want to replace in your application.
implements FragmentDrawer.FragmentDrawerListener
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
</LinearLayout>
<FrameLayout
android:id="@+id/container_body"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
<fragment
android:id="@+id/fragment_navigation_drawer"
android:name="com.archi.intrisfeed.slidemenu.FragmentDrawer"
android:layout_width="@dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_navigation_drawer"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
@Override
public void onDrawerItemSelected(View view, int position) {
displayView(position);
}
private void displayView(int position) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (String.valueOf(position)) {
case "0":
fragment = new HomeFragment();
break;
case "1":
fragment = new BrowseFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
// set the toolbar title
getSupportActionBar().setTitle(title);
}
实现FragmentDrawer.FragmentDrawerListener
activity_main.xml
@凌驾
选定的绘图区域上的公共void(视图,int位置){
显示视图(位置);
}
私有void显示视图(内部位置){
片段=空;
String title=getString(R.String.app\u name);
开关(字符串值(位置)){
案例“0”:
fragment=新的HomeFragment();
打破
案例“1”:
fragment=新的BrowseFragment();
打破
违约:
打破
}
if(片段!=null){
FragmentManager FragmentManager=getSupportFragmentManager();
FragmentTransaction FragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container\u body,fragment);
fragmentTransaction.commit();
//设置工具栏标题
getSupportActionBar().setTitle(标题);
}