【发布时间】: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;
}
}
我整天都在尝试制作一个底部导航栏,并根据导航栏中的按钮切换片段,但我没能做到。 我看过无数视频并尝试了我能找到的任何东西。请帮我。
【问题讨论】: