首页 > 解决方案 > 如何从 BottomSheet 打开片段

问题描述

您好,我已经实现了一个bottomsheetdilooge,我希望当在底部表中按下textview时,应该打开一个片段,请您告诉我如何实现它并且还想添加BackStack,当按下注销时我想打开一个dilouge而不是片段

这是我的代码

Profile_Fragment.java // 单击 imageview 时底部表打开的位置

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_profile, container, false);
    requireActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    view.findViewById(R.id.snipet_profile);
    ImageView accountSettings = view.findViewById(R.id.account_Settings);
    // i open the bottom sheet when the imageview is clicked
    accountSettings.setOnClickListener(
            v -> {
                BottomSheet bottomSheet = new BottomSheet();
                bottomSheet.show(requireActivity().getSupportFragmentManager(), "ModalBottomSheet");
            }
    );

BottomSheet.java // 我没有在这里实现任何东西

public class BottomSheet extends BottomSheetDialogFragment {

    public BottomSheet() {
    }

    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.bottom_sheet_profile, container, false);
    }
}

bottom_sheet_profile.xml

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

    <View
        android:id="@+id/slidedownview"
        android:layout_width="40dp"
        android:layout_height="3dp"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:background="@drawable/rounded_corner_bottomsheetline"
        android:layout_marginTop="11dp"/>

    <TextView
        android:id="@+id/settings"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/slidedownview"
        android:layout_alignParentStart="true"
        android:layout_marginStart="30dp"
        android:layout_marginTop="20dp"
        android:elevation="10dp"
        android:text="@string/settings"
        android:textColor="@color/white"
        android:textSize="20sp"
        android:textStyle="normal" />

    <TextView
        android:id="@+id/edit_profile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/settings"
        android:layout_alignParentStart="true"
        android:layout_marginStart="30dp"
        android:layout_marginTop="17dp"
        android:elevation="10dp"
        android:text="@string/edit_profile"
        android:textColor="@color/white"
        android:textSize="20sp"
        android:textStyle="normal" />

    <TextView
        android:id="@+id/favorite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edit_profile"
        android:layout_alignParentStart="true"
        android:layout_marginStart="30dp"
        android:layout_marginTop="17dp"
        android:elevation="10dp"
        android:text="@string/favorite"
        android:textColor="@color/white"
        android:textSize="20sp"
        android:textStyle="normal" />

    <TextView
        android:id="@+id/log_out"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/favorite"
        android:layout_alignParentStart="true"
        android:layout_marginStart="30dp"
        android:layout_marginTop="17dp"
        android:layout_marginBottom="15dp"
        android:elevation="10dp"
        android:text="@string/log_out"
        android:textColor="@color/white"
        android:textSize="20sp"
        android:textStyle="normal" />


</RelativeLayout>

标签: androidandroid-fragmentsbottom-sheetandroid-bottomsheetdialog

解决方案


推荐阅读