在不同片段之间导航时,BottomSheet 不会隐藏 [英] BottomSheet is not hiding when navigating between different fragments

查看:19
本文介绍了在不同片段之间导航时,BottomSheet 不会隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一个 bottomsheet 和一些 textViews(作为按钮),按下它会导航到一个不同的片段,但问题是当 textView 被按下并导航到片段仍然没有隐藏底部工作表,因此必须点击屏幕才能隐藏底部工作表,我希望在启动片段时底部工作表将隐藏,这是我遇到什么问题的屏幕录制 链接

Hello there I have a bottomsheet with some textViews (as button) when pressed it navigates to a diffrent fragment but the issue is when textView is pressed and navigated to the fragment still the bottom sheet is not hiding evantually have to tap on the screen for hiding the bottom sheet , I want that when the fragment is launched the bottom sheet will hide, here is a screen recording of what issue im getting 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 时正在打开的片段

Edit_Profile.java // the fragment which is being open when textView is pressed

  @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.

Your BottomSheet is hosted by a BottomSheetDialogFragment. Internally it has a Dialog that can be dismissed using dismiss() which will eventually dismiss the BottomSheet.

在提交片段事务时将其应用到您的代码中:

Applying that in your code whenever a fragment transaction is committed:

    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
    });

这篇关于在不同片段之间导航时,BottomSheet 不会隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆