我想从片段中打开BottomSheetDialog。在此之前,我尝试在 Activity 时打开BottomSheetDialog并成功。我使用相同的代码,并将getSupportFragmentManager替换为getChildFragmentManager。我还尝试用getActivity().getSupportFragmentManager替换getSupportFragmentManager。有解决方案吗?

public class MainFragment extends Fragment implements BottomSheetDialog.BottomSheetListener {

private TextView mTextView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_beranda, container, false);
    Button buttonOpenBottomSheet = findViewById(R.id.button_open_bottom_sheet);
    buttonOpenBottomSheet.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            BottomSheetDialog bottomSheet = new BottomSheetDialog();
            bottomSheet.show(getChildFragmentManager(),"BottomSheet");
        }
    });
 }
}

最佳答案

尝试BottomSheetDialogFragment

ModalBottomSheetDialogFragment.Builder()
.add(R.menu.menu_options)
.show(childFragmentManager,“my_bottom_sheet”)

关于android-fragments - 如何在片段内打开BottomSheetDialog,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54124650/

10-11 14:02