Home > OS >  How to closed BottomSheetDialog when user click item in RecyclerView?
How to closed BottomSheetDialog when user click item in RecyclerView?

Time:10-29

I'm working with project where user can redirect to another page by selecting item in bottomsheet.

This is sample code for Fragment.java

private BottomSheetDialog bottomsheet;

private void displayBottomSheet(){
   bottomsheet.show
   View view = getLayoutInflater().inflate(R.layout.bottom_sheet, null, false);
   RecyclerView recyclerView = view.findViewById(R.id.RecyclerView);

   LinearLayoutManager linearLayoutManager = new 
   LinearLayoutManager(getContext().getApplicationContext());
   recyclerView.setLayoutManager(linearLayoutManager);
   RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(getContext(), 
   ..., ..., ...);
   recyclerView.setAdapter(recyclerViewAdapter);

   bottomsheet.setContentView(view);
}

This is sample code for my RecyclerViewAdapter

private void openFragmentScreen(int position, View view) {
  // This is where the recyclerview item click listener to redirect in another fragment
}

I have done:

  • recyclerview.onClickListener but its not working

Is there a way to call bottomsheetdialog in adapter class so I can easily use bottomsheet.dismiss() in my openFragmentScreen()?

CodePudding user response:

The question is not quiet clear but you have 2 ways to interact with your adapter class

(this is not the right way but it works in this situation)

  1. just pass the instance of your dialog to the constructor of your Adapter Class and inside onbind method use that dialog instance to dismiss it

  2. create an interface, take it as a constructor argument in your adapter class, call the function whenever your activity/fragment to be notified, and then when ever you want to create an instance of your adapter class, let the class implement the interface by passing "this" instead of that interface to your adapter instance.

  • Related