【问题标题】:Fragments are not getting replaced in the main_activity()片段没有在 main_activity() 中被替换
【发布时间】:2016-07-30 22:50:29
【问题描述】:

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

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

主要活动sn-p:

    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;
}
}

【问题讨论】:

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


    【解决方案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"
    

    【讨论】:

    • 兄弟我试过你的解决方案,但它没有改变输出。它仍然是同一个片段,一个布局没有被替换为活动主的线性布局。
    • 我还在线性布局中添加了一个文本视图,我想用片段替换它。在我刚刚写的文本视图中,这是主要活动。当我单击导航抽屉项目运行应用程序后,“这是主要活动”仍然存在。所以我想问题是片段没有被替换。
    • 你必须像这样在主布局中进行框架布局 它会完成这项工作
    • 替换一个 FrameLayput 而不是线性布局
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多