如何在事务到另一个片段后删除第一个片段的布局?

How to remove the layout of the first fragment after transaction to another fragment?

我想将用户从一个片段重定向到另一个片段,但问题是我将 2 个片段布局放在彼此之上。 所以我有一个包含回收器视图的第一个片段,如下所示: 我想在用户按下回收站视图中的文本时将用户重定向到另一个片段。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="wrap_content"
android:id="@+id/nav_admin_seller"
tools:context=".Admin.Seller.AdminSellerFragment">

<TextView
    android:id="@+id/tv_date_admin_seller"
    android:layout_width="wrap_content"
    android:layout_height="25dp"
    android:layout_marginTop="8dp"
    android:gravity="center"
    android:textColor="@color/logo_color"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rv_admin_seller"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="48dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tv_date_admin_seller" />

</FrameLayout>

这是我实现 onClickListenener 的适配器部分

@RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onBindViewHolder(AdminSellerAdapter.ViewHolder holder, @SuppressLint("RecyclerView") int position) {

        AdminSeller item = itemList.get(position);
        holder.name.setText(String.valueOf(item.getUsername()));
        holder.password.setText(String.valueOf(item.getPassword()));
        holder.daliycash.setText(String.valueOf(item.getDaliyCash()));
        holder.totalcash.setText(String.valueOf(item.getTotalCash()));


        holder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("call.myfragment.action");
                id = String.valueOf(itemList.get(position).getSellerId())+"/"+current_date;
                intent.putExtra("id",id);
                mCtx.sendBroadcast(intent);
            }
        });

    }

然后我在包含 2 个片段的 MainActivity 中添加一个广播接收器:

BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String id = intent.getStringExtra("id");
                Bundle arguments = new Bundle();
                arguments.putString( "id" , id);

                //arguments.putBoolean("cameFromSeller1",true);
                Fragment frag = null;
                frag = new AdminSellerMoreInfo();
                frag.setArguments(arguments);
                //Fragment fragment = new AdminSellerFragment();
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                ft.replace(R.id.nav_admin_seller, frag);
                //ft.detach(fragment);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.addToBackStack(null);
                ft.commit();
            }
        };
        Admin.this.registerReceiver(mBroadcastReceiver, new IntentFilter("call.myfragment.action"));

这是第二个片段的 Java 代码:

public class AdminSellerMoreInfo extends Fragment {
    View root;
    private String sellerid;

    private List<SellerOutStockItem> itemList1 = new ArrayList<>();
    private RecyclerView recyclerView1;
    private ADMINSELLERADAPTER_ITEM_QUENTITY adapter1;

    public AdminSellerMoreInfo() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        root = inflater.inflate(R.layout.fragment_admin_seller_more_info, container, false);
        recyclerView1 = root.findViewById(R.id.rv_admin_seller_items_name_q);
        Bundle arguments = getArguments();
        if (arguments != null) {
            sellerid = arguments.getString("id");
            String id1 = sellerid;
        }
        String[] split = sellerid.split("/");
        getitems_name(split[0],split[1]);
        return root;
    }
}

这是第二个片段的布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/nav_admin_seller2"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_admin_seller_items_name_q"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="1dp"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="1dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</FrameLayout>

在你写的MainActivity中广播接收器的onReceive()方法中-

 ft.replace(R.id.nav_admin_seller, frag);

第一个参数是id,在这里你应该把根布局的id放在你的main_activity.xml中,这是执行片段事务时这些片段的实际容器。