在不同片段之间导航时 BottomSheet 没有隐藏

BottomSheet is not hiding when navigating between different fragments

你好,我有一个 bottomsheet 和一些文本视图(作为按钮),按下它会导航到不同的片段,但问题是当 textView 被按下并导航到仍然位于底部的片段时sheet 最终没有隐藏必须点击屏幕才能隐藏底部 sheet ,我希望当片段启动时底部 sheet 会隐藏,这是屏幕录制的内容问题我得到 link

Profile_Fragment.java

 ImageView accountSettings = view.findViewById(R.id.account_Settings);
  accountSettings.setOnClickListener(
                v -> {
                    BottomSheet bottomSheet = new BottomSheet();
                    bottomSheet.show(requireActivity().getSupportFragmentManager(), bottomSheet.getTag());
                }
        );

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) {
        View view = inflater.inflate(R.layout.bottom_sheet_profile, container, false);
       

        TextView settingsProfileTextView = view.findViewById(R.id.settings);
        settingsProfileTextView.setOnClickListener(v -> {
            Fragment settings_profile = new Settings_Profile();
            FragmentTransaction transaction = getParentFragmentManager().beginTransaction();
            transaction.add(R.id.fragment_container, settings_profile);
            transaction.addToBackStack(String.valueOf(settings_profile));
            transaction.commit();
          
        });
        TextView editProfileTextView = view.findViewById(R.id.edit_profile);
        editProfileTextView.setOnClickListener(v -> {
            Fragment edit_profile = new Edit_Profile();
            FragmentTransaction transaction = getParentFragmentManager().beginTransaction();
            transaction.add(R.id.fragment_container, edit_profile);
            transaction.addToBackStack(String.valueOf(edit_profile));
            transaction.commit();
        });
        return view;
    }
}

Edit_Profile.java // 按下textView时打开的片段

  @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_edit_profile, container, false);
        profilePhoto = view.findViewById(R.id.circleImageView);
        initImageLoader();
        setProfileImage();
        ImageView imageView = view.findViewById(R.id.backArrow);
        imageView.setOnClickListener(v -> {
            Fragment newCase = new Profile_Fragment();
            assert getFragmentManager() != null;
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.fragment_container, newCase);
            transaction.disallowAddToBackStack();
            transaction.commit();
        });

        return view;
    }

您的 BottomSheetBottomSheetDialogFragment 托管。 在内部它有一个 Dialog 可以使用 dismiss() 关闭它最终将关闭 BottomSheet.

每当提交片段事务时在您的代码中应用它:

    settingsProfileTextView.setOnClickListener(v -> {
        Fragment settings_profile = new Settings_Profile();
        FragmentTransaction transaction = getParentFragmentManager().beginTransaction();
        transaction.add(R.id.fragment_container, settings_profile);
        transaction.addToBackStack(String.valueOf(settings_profile));
        transaction.commit();
        dismiss(); // Dismiss the Bottom sheet
      
    });
    TextView editProfileTextView = view.findViewById(R.id.edit_profile);
    editProfileTextView.setOnClickListener(v -> {
        Fragment edit_profile = new Edit_Profile();
        FragmentTransaction transaction = getParentFragmentManager().beginTransaction();
        transaction.add(R.id.fragment_container, edit_profile);
        transaction.addToBackStack(String.valueOf(edit_profile));
        transaction.commit();
        dismiss(); // Dismiss the Bottom sheet
    });