1

我正在处理底部导航栏,我已经替换了 floatingActionButton 的 onClickListener 上的片段,但是如果选择了底部导航栏中的其他项目并且一次显示两个片段,则不会删除该片段。这是我的输出

在此处输入图像描述

如果选择了底部导航栏中的其他项目,我如何删除这个浮动操作按钮的帖子片段。

这是我的 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<fragment
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toTopOf="@id/nav_view"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:navGraph="@navigation/mobile_navigation" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/app_name"
    app:backgroundTint="@color/white"
    app:layout_anchor="@id/bottomAppBar"
    app:maxImageSize="64dp"
    app:srcCompat="@drawable/ic_baseline_add_circle_24" />

<com.google.android.material.bottomappbar.BottomAppBar
    android:id="@+id/bottomAppBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    app:fabAlignmentMode="center"
    android:layout_gravity="bottom"
    app:contentInsetStart="0dp"
    app:fabCradleMargin="0dp"
    app:fabCradleRoundedCornerRadius="0dp"
    app:fabCradleVerticalOffset="0dp">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:labelVisibilityMode="labeled"
        app:menu="@menu/bottom_nav_menu" />
</com.google.android.material.bottomappbar.BottomAppBar>

 </androidx.coordinatorlayout.widget.CoordinatorLayout>

这是我的 MainActivity.java

package www.example.xchangeit;


import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.viewpager.widget.ViewPager;

import www.example.xchangeit.ui.post.PostFragment;

public class MainActivity extends AppCompatActivity{


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.opening_page);
    ViewGroup LiView = findViewById(R.id.ViewGroup);
    LiView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        layout();

        }
    });

    ImageView imageView = findViewById(R.id.logo_imageView);
    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            layout();
        }
    });


    }

    private void layout()
    {
        setContentView(R.layout.activity_main);
        BottomNavigationView navView = findViewById(R.id.nav_view);

        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_exchange, R.id.navigation_post,R.id.navigation_chat,R.id.navigation_account)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.nav_host_fragment,new PostFragment());
                transaction.commit();}

        });

    }

   }
4

1 回答 1

0

您可以在加载下一个之前弹出上一个片段

getSupportFragmentManager().popBackStack();

现在,如果您需要更具体,可以执行以下操作:

getSupportFragmentManager().beginTransaction().remove(frag).commit(); 

在第二种情况下,我们保留对要删除的片段的引用。

于 2021-08-20T07:44:21.290 回答