gpt4 book ai didi

java - 替换底部导航中的 fragment

转载 作者:行者123 更新时间:2023-12-02 01:52:05 27 4
gpt4 key购买 nike

我已经创建了一个底部导航,但当我在底部导航中选择该选项时,该 fragment 并未将其自身替换为另一个 fragment 。这是我的代码。任何帮助将不胜感激。这是我在 android studio 项目中编写的代码,我不知道为什么它没有加载 fragment 。

主要 Activity

package com.divesh.ss;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;


public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

loadFragment(new dash());
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setSelectedItemId(R.id.dashboard);
}

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment;
switch (item.getItemId()) {

case R.id.settings:
fragment = new settings();
loadFragment(fragment);
return true;
case R.id.work:
fragment = new work();
loadFragment(fragment);
return true;
case R.id.donate:
fragment = new donate();
loadFragment(fragment);
return true;
case R.id.team:
fragment = new team();
loadFragment(fragment);
return true;
}

return false;
}
};

private void loadFragment(Fragment fragment) {
// load fragment
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}

}

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.divesh.ss.MainActivity">

<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:itemBackground="@color/colorPrimary"
android:foreground="?attr/selectableItemBackground"
app:itemIconTint="@android:color/white"
app:itemTextColor="@android:color/white"
app:menu="@menu/navigation" />

</android.support.design.widget.CoordinatorLayout>

最佳答案

您为 BottomView 导航创建了监听器,但没有设置它。

您需要为导航设置监听器:

navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

关于java - 替换底部导航中的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52821624/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com