简体   繁体   中英

How to use bottom sheet in fragment class instead of Activity Class?

I am trying to use the bottom sheet in a click listener but I am getting an error on this line.

bottomSheetFragment.show(getSupportFragmentManager())

Cannot resolve method 'show(?, java.lang.String)' Cannot resolve method 'getSupportFragmentManager()

I want to use the bottom sheet in a fragment class.

SubCategoryDetailFragment.java

  public class SubCategoryDetailFragment extends Fragment {

        TextView txtv_sort;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view= inflater.inflate(R.layout.fragment_sub_category_detail, container, false);
            toolbar = ((MainActivity) getActivity()).findViewById(R.id.toolbar);
            toggle = ((MainActivity) getActivity()).getToggle();
            shimmerContainer = view.findViewById(R.id.shimmer_view_container);
            recyclerView_subcatDetail = view.findViewById(R.id.recycler_view_subCategoryDetail);

           txtv_sort = view.findViewById(R.id.txtv_sort);

            toggle.setDrawerIndicatorEnabled(false);
            toggle.setHomeAsUpIndicator(R.drawable.back);
            toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getActivity().onBackPressed();
                }
            });


         txtv_sort.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    BottomSheetFragment bottomSheetFragment = new BottomSheetFragment();
                    bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());
                }
            });

            return view;
        }

    }

BottomSheetFragment.java

public class BottomSheetFragment extends Fragment {


    public BottomSheetFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_bottom_sheet, container, false);
    }

}

fragment_bottom_sheet.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="8dp"
    android:paddingTop="8dp"
    tools:context=".Fragments.BottomSheetFragment">

    <!-- NOTE: This list should be displayed in a list
    instead of nested layouts -->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:foreground="?attr/selectableItemBackground"
        android:orientation="horizontal"
        android:paddingBottom="8dp"

        android:paddingTop="8dp">

        <ImageView
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginRight="32dp"
            android:src="@drawable/ic_launcher_background"
            android:tint="#737373" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Preview"
            android:textColor="#737373"
            android:textSize="16sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:foreground="?attr/selectableItemBackground"
        android:orientation="horizontal"
        android:paddingBottom="8dp"

        android:paddingTop="8dp">

        <ImageView
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginRight="32dp"
            android:src="@drawable/ic_launcher_background"
            android:tint="#737373" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Share"
            android:textColor="#737373"
            android:textSize="16sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:foreground="?attr/selectableItemBackground"
        android:orientation="horizontal"
        android:paddingBottom="8dp"

        android:paddingTop="8dp">

        <ImageView
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginRight="32dp"
            android:src="@drawable/ic_launcher_background"
            android:tint="#737373" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Get link"
            android:textColor="#737373"
            android:textSize="16sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:foreground="?attr/selectableItemBackground"
        android:orientation="horizontal"
        android:paddingBottom="8dp"

        android:paddingTop="8dp">

        <ImageView
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginRight="32dp"
            android:src="@drawable/ic_launcher_background"
            android:tint="#737373" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Make a Copy"
            android:textColor="#737373"
            android:textSize="16sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:foreground="?attr/selectableItemBackground"
        android:orientation="horizontal"
        android:paddingBottom="8dp"

        android:paddingTop="8dp">

        <ImageView
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginRight="32dp"
            android:src="@drawable/ic_launcher_background"
            android:tint="#737373" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Email a Copy"
            android:textColor="#737373"
            android:textSize="16sp" />
    </LinearLayout>
</LinearLayout>

Yes, you can use BottomSheetDialogFragment in Fragment simply like below code,

BottomSheetDialogFragment bottomSheetFragment = new YourBottomSheetFragmentClass();
bottomSheetFragment.show(getFragmentManager(), bottomSheetFragment.getTag());

Or if you want to pass some data to BottomSheetDialogFragment use below code, with newInstance you can send and retrieve data.

Inside your fragment class :

BottomSheetDialogFragment myBottomSheet = YourBottomSheetFragmentClass.newInstance(SendString);            
myBottomSheet.show(getFragmentManager(),myBottomSheet.getTag());

In your BottomSheetFragment class, add below line

static YourBottomSheetFragmentClass newInstance(String retrieveString) {
        YourBottomSheetFragmentClass f = new YourBottomSheetFragmentClass();
        Bundle args = new Bundle();
        args.putString("getString", retrieveString);
        f.setArguments(args);
        return f;
            return new f();
        }

Also extends your YourBottomSheetFragmentClass with BottomSheetDialogFragment

According to documentation in order to pass data between parent and child fragment do this: to launc

to launch the bottom sheet:

button.setOnClickListener{
    val sortRecipesBottomSheet = SortRecipesBottomSheet()
    sortRecipesBottomSheet.show(childFragmentManager,sortRecipesBottomSheet.tag)
}

in the same fragment write this code to receive the result from the bottom sheet

 childFragmentManager.setFragmentResultListener(
            "requestKey",
            viewLifecycleOwner
        ) { key, bundle ->
            val result = bundle.getString("bundleKey")
            println("heeeeeeere: "+ result)
            // Do something with the result
        }

in your bottomsheet fragment in order to send the result back write this:

 view.findViewById<MaterialButton>(R.id.sortByName).setOnClickListener {
            parentFragmentManager.setFragmentResult(
                "requestKey",
                bundleOf("bundleKey" to SortType.ByName.key)
            )
            dismiss()
        }

in parent fragment in show() method you are sending childFragmentManager and you are using childFragmentManager to listen to the result and in bottomsheet you are using parentFragmentManager to set the result for the parent fragment to read.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM