如何使用导航从bottomsheet对话框导航到片段?

问题描述 投票:0回答:1

我目前正在制作一个具有底表功能的应用程序,当用户在对话框中点击一个图标(图像视图)时,该片段将显示出来。我的问题是我无法导航到片段b,并且返回此错误enter image description here

这是我的家庭片段,是活动启动时首先开始的片段

public class HomeFrag extends Fragment {

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home, container, false);
    ImageView buttonOpenSheet = view.findViewById(R.id.buttonopensheet);

    buttonOpenSheet.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            BottomSheetDialog bottomsheet = new BottomSheetDialog();
            bottomsheet.show(getFragmentManager(), "bottomsheet");
        }
    });

    return view;
}}

bottomsheetdialog.java

public class BottomSheetDialog extends BottomSheetDialogFragment {

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.bottom_sheet, container, false);

    //Views
    ImageView usual = view.findViewById(R.id.usual);
    final NavController navigator = 
Navigation.findNavController(getActivity(), R.id.fragment_host);

    usual.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            navigator.navigate(R.id.action_bottomSheetDialog_to_usualFrag);
            Log.d(TAG, "user clicked" );
        }
    });
    return view;
}}

这是我的导航

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/app_nav"
app:startDestination="@id/homeFrag">

<fragment
    android:id="@+id/homeFrag"
    android:name="com.bw.f22920.HomeFrag"
    android:label="fragment_home"
    tools:layout="@layout/fragment_home" />

<dialog
    android:id="@+id/bottomSheetDialog"
    android:name="com.bw.f22920.BottomSheetDialog"
    android:label="bottom_sheet"
    tools:layout="@layout/bottom_sheet">
    <action
        android:id="@+id/action_bottomSheetDialog_to_usualFrag"
        app:destination="@id/usualFrag" />
</dialog>
<fragment
    android:id="@+id/usualFrag"
    android:name="com.bw.f22920.UsualFrag"
    android:label="fragment_usual"
    tools:layout="@layout/fragment_usual" />


</navigation>
android uinavigationcontroller android-jetpack navigationcontroller
1个回答
0
投票

您正在使用

@Override
public void onClick(View v) {
    BottomSheetDialog bottomsheet = new BottomSheetDialog();
    bottomsheet.show(getFragmentManager(), "bottomsheet");
}

这意味着NavController不知道您当前的目的地已更改为底部对话框。

您需要致电navigate()

@Override
public void onClick(View v) {
    Navigation.findNavController(v).navigate(R.id.bottomSheetDialog);
}
© www.soinside.com 2019 - 2024. All rights reserved.