java - fragment 不会在 main_activity() 中被替换

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

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

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

主要 Activity fragment :

    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>

编写一个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;
}
}

最佳答案

在开关中使用它

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"

关于java - fragment 不会在 main_activity() 中被替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38674596/

相关文章:

android - 有没有在android中创建Hmac256字符串的函数?

android:如何将填充添加到 FrameLayout 内的 LinearLayout

android - 使用adapterview时无法在 View 组中绘制 subview

java - 找不到变量(继承问题)

java - 列的值太长 - 错误持续存在 LocalDate

java - 创建数字音频工作站

android - 如何引导用户单击 View ?

java - 导入eclipse项目卡住

android - 停止 Observable.interval 并发送 onComplete

android - 使用 ListView 时如何修复底部标签栏