如何在片段内打开BottomSheetDialog

时间:2019-01-10 08:35:18

标签: android-fragments bottom-sheet

我想从片段中打开BottomSheetDialog。在此之前,我尝试在活动时打开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");
        }
    });
 }
}

1 个答案:

答案 0 :(得分:0)

尝试BottomSheetDialogFragment

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