片段没有被main_activity()替换

时间:2016-07-30 13:43:13

标签: java android android-layout android-fragments navigation-drawer

我正在尝试使用引用链接使用片段制作导航抽屉:https://guides.codepath.com/android/Fragment-Navigation-Drawer

但问题是当我点击导航抽屉中的任何项目时,它不会将相应的片段替换为主要活动布局。

主要活动摘录:

    public void selectDrawerItem(MenuItem menuItem)  {
    // Create a new fragment and specify the fragment to show based on nav item clicked
    Fragment fragment = null;
    Class fragmentClass;
    switch(menuItem.getItemId()) {

        case R.id.nav_first_fragment:

            //fragmentClass = FirstFragment.class;
            fragment=new FirstFragment();
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().add(R.id.flContent, fragment).commit();
            break;

        case R.id.nav_second_fragment:
            //fragmentClass = SecondFragment.class;
            fragment=new SecondFragment();
            FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
            tx.replace(R.id.flContent, Fragment.instantiate(MainActivity.this, "com.example.FirstFragment"));
            tx.commit();
            break;

        default:
            fragmentClass = FirstFragment.class;
    }    

activity_main布局:     

<!-- This LinearLayout represents the contents of the screen  -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<!-- The ActionBar displayed at the top -->
<include
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />



<!-- The main content view where fragments are loaded -->
<LinearLayout
android:id="@+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
        />
</LinearLayout>
<!-- The navigation drawer that comes from the left -->
<!-- Note that `android:layout_gravity` needs to be set to 'start' -->
<android.support.design.widget.NavigationView
android:id="@+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="@layout/nav_header"
android:layout_gravity="start"
android:background="@android:color/white"
app:menu="@menu/drawer_view" />
</android.support.v4.widget.DrawerLayout>

fragment one layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment 1"
android:layout_gravity="center"
android:gravity="center"
android:background="#FF5722"/>
</LinearLayout>

frament one java:

public class FirstFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View view = inflater.inflate(R.layout.home, container,false);
if(container==null){
Log.d("First","Null");
return view;
}Log.d("First"," Not Null");
return null;
}
}

1 个答案:

答案 0 :(得分:1)

在开关中使用它

switch(menuItem.getItemId()) {

 case R.id.nav_first_fragment:
      getSupportFragmentManager().beginTransaction().replace(R.id.container, new FirstFragment()).commit();
      break;

 case R.id.nav_second_fragment:
      getSupportFragmentManager().beginTransaction().replace(R.id.container, new SecondFragment()).commit();
      break;

}

并确保为交换机提供正确的ID。 “container”是activity_main.xml

布局的ID

只需将ID提供给主要布局,如

android:id="@+id/container"