Android Fragments 片段不会在Android应用程序上更改

vfwfrxfs  于 2023-02-04  发布在  Android
关注(0)|答案(1)|浏览(97)

尊敬的堆栈溢出程序员,我对编程世界非常陌生,实际上正在尝试构建一个应用程序来帮助我的医学院同学。
在我的应用程序中,我似乎不能在按下导航栏上的按钮后更改片段。最初只有一个片段加载(主页片段),其余的片段根本没有更改。由于我是编程世界的新手,这可能只是我的一个小错误,但我希望你们都能指导我。
下面是我的MainActivity java文件

public class MainActivity extends AppCompatActivity {
    BottomNavigationView bnView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bnView=findViewById(R.id.bnView);
        bnView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int id=item.getItemId();
                if (id==R.id.home) {
                    loadfrag(new Homefragment(), true);
                }else if(id==R.id.subject_based) {
                    loadfrag(new subjectfragment(), false);
                }else if(id==R.id.about) {
                    loadfrag(new aboutfragment(), false);
                }else if(id==R.id.exam) {
                    loadfrag(new examfragment(), false);
                }else {
                    loadfrag(new paperfragment(), false);
                }
                return true;
            }
        });
        bnView.setSelectedItemId(R.id.home);
    }
    public void loadfrag(Fragment fragment, boolean flag){
        FragmentManager fm=getSupportFragmentManager();
        FragmentTransaction ft= fm.beginTransaction();
        if (flag)
            ft.add(R.id.container,fragment);
        else
            ft.replace(R.id.container,fragment);
        ft.commit();
    }
}

我的主要活动xml文件如下;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/goojasto"
    tools:context="com.cringyprithak.mcqrunch_3.MainActivity">
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="?attr/actionBarSize"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bnView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#EFD115"
        app:menu="@menu/nav_items" />
</RelativeLayout>

我的菜单如下

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/subject_based"
        android:icon="@drawable/ic_sub"
        android:title="Subject-wise"/>
    <item
        android:id="@+id/paper"
        android:icon="@drawable/ic_paper"
        android:title="Paper-wise"/>
    <item
        android:id="@+id/home"
        android:icon="@drawable/ic_home"
        android:title="Home"/>
    <item
        android:id="@+id/exam"
        android:icon="@drawable/ic_exam"
        android:title="Exam Practice"/>
    <item
        android:id="@+id/about"
        android:icon="@drawable/ic_action_name"
        android:title="About Me"/>

</menu>

我的代码片段中的一个例子是

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class aboutfragment extends Fragment {
    View view;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view= inflater.inflate(R.layout.fragment_homefragment, container, false);
        return view;
    }
}

我一直在尝试做一个底部导航栏,并根据导航栏中的按钮切换片段,但我一直无法做到。我看了无数的视频,并尝试了我能找到的任何东西。请帮助我。

bbmckpt7

bbmckpt71#

您的代码总体上没有问题。使用BottomNavigationBar是在Fragment之间切换的正确方法。
只是对下面的函数做了一个小改动,在Fragment之间切换时可以直接使用replace()

// Removed the boolean flag and use replace() directly
    public void loadfrag(Fragment fragment) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        // Can remove the add() function and just use replace()
        ft.replace(R.id.container, fragment);
        ft.commit();
    }

因此,我猜您遇到的问题是当您尝试将视图加载到Fragment时。
对于您的aboutfragment,您正在尝试加载R.layout.fragment_homefragment。从命名来看,它似乎是一个HomeFragment布局,而不是您的aboutfragment布局。您应该在那里有一个您的aboutfragment布局:

public class aboutfragment extends Fragment {
    View view;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Should replace R.layout.fragment_homefragment with your aboutfragment layout
        view = inflater.inflate(R.layout.fragment_homefragment, container, false);
        return view;
    }
}

为了更好地练习,请在创建函数或类时遵循Java Naming Convention
例如:

  • Homefragment应位于HomeFragment
  • aboutfragment应该在AboutFragment

相关问题