Skip to content
Advertisement

How to dismiss Bottomsheet on Click in ViewHolder RecyclerView Adapter?

I want to dismiss my BottomSheet on Click of cardView in BottomSheet

Here is my code of onBindViewHolder

 class Viewholder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private TextView num_name;
        private TextView call_type;
        private TextView call_duration;
        public Viewholder(@NonNull View itemView) {
            super(itemView);
            num_name = itemView.findViewById(R.id.name_number);
            call_type = itemView.findViewById(R.id.call_type_txt);
            call_duration = itemView.findViewById(R.id.call_duration_txt);

            itemView.setOnClickListener(this);
        }

        private void setData(String name_num, String call_typ, String call_dur){
            num_name.setText(name_num);
            call_type.setText(call_typ);
            call_duration.setText(call_dur);
        }

        @Override
        public void onClick(View view) {
            Toast.makeText(view.getContext(),"clicked", Toast.LENGTH_SHORT).show();
            BottomSheetCall bottomSheetCall = new BottomSheetCall();
            bottomSheetCall.dismiss();
        }
    }

I getting this error

java.lang.IllegalStateException: Fragment BottomSheetCall{31fd510 (dbba722a-e3f4-46d4-826c-f31cbc221bd6)} not associated with a fragment manager.
        at androidx.fragment.app.Fragment.requireFragmentManager(Fragment.java:910)
        at androidx.fragment.app.DialogFragment.dismissInternal(DialogFragment.java:245)
        at androidx.fragment.app.DialogFragment.dismiss(DialogFragment.java:202)
        at com.google.android.material.bottomsheet.BottomSheetDialogFragment.dismiss(BottomSheetDialogFragment.java:47)
        at com.bizcure.waessentials.ui.main.CallAdapter$1.onClick(CallAdapter.java:53)

onClick I want to dismiss Bottomsheet and backto Activity.

Thank you in advance =)

Advertisement

Answer

what you have done here will not work as you expect it to be :-

BottomSheetCall bottomSheetCall = new BottomSheetCall();
bottomSheetCall.dismiss();

Here you are creating a new bottomSheet Object so using dismiss() on this object will not work. Instead you need a interface which will provide you a callback to your bottomShett Fragment where you have setup your recyclerView. And in that Callback you need to call simply call BottomSheet inbuild method dismiss(). Here is the answer on S.O which shows how you can create that interface which will provide you callback to your Fragment.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement