【问题标题】:Fragments won't change on Android applicationFragments 不会在 Android 应用程序上改变
【发布时间】:2023-02-03 09:57:15
【问题描述】:

尊敬的堆栈溢出程序员, 我对编程世界还很陌生,实际上我正在尝试构建一个应用程序来帮助我的医学同学。

在我的应用程序中,我似乎无法在按下导航栏上的按钮后更改片段。首先只加载一个片段(主页片段),其余的根本不会改变。 由于我是编程界的新手,这可能只是我的一个小错误,但我希望大家能指导我。

这是我的 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;
    }
}

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

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

    您的代码总体上很好。这是在两者之间切换的正确方法分段通过使用底部导航栏.

    只是以下功能的一个小改动。切换时可以直接使用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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-21
      • 2012-07-14
      • 1970-01-01
      相关资源
      最近更新 更多