简体   繁体   English

FragmentActivity和导航抽屉

[英]android - FragmentActivity and Navigation Drawer

Problem : When I click an item on the drawer it doesn't show the FragmentActivity that is supposed to be shown. 问题:当我单击抽屉上的某个项目时,它没有显示应该显示的FragmentActivity。 App is ok it doesn't have errors when I compile it and It doesn't crash when I click an item in the drawer 应用程序正常,编译时没有错误,单击抽屉中的项目也不会崩溃

MainActivty 主活动

private void displayView(int position) {
    // update the main content by replacing fragments
    FragmentActivity fragment = null;
    switch (position) {
    case 0:
        fragment = new HomeFragment();
        break;
    case 1:
        fragment = new LocateFragment();
        break;
    case 2:
        fragment = new SecureFragment();
        break;
    case 3:
        fragment = new NotificationFragment();
        break;

    default:
        break;
    }

    if (fragment != null) {
        LayoutInflater inflater = getLayoutInflater();
        LinearLayout container = (LinearLayout)findViewById(R.id.frame_container);
        inflater.inflate(R.layout.activity_main,container);
        //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");
    }
}  

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 -->
<LinearLayout
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" />

<!-- 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:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"        
    android:listSelector="@drawable/list_selector"
    android:background="@color/list_background"/>

if (fragment != null) {
  FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  transaction.add(R.id.frame_container,fragment);
  transaction.commit();
  mDrawerList.setItemChecked(position, true);
  mDrawerList.setSelection(position);
  setTitle(navMenuTitles[position]);
  mDrawerLayout.closeDrawer(mDrawerList);
} 

I guess you are after something like this. 我想你是在追求这样的事情。 You will with this put the fragment in the container view. 您将以此将片段放入容器视图中。 Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM