我正在开发一个项目,用户可以通过选择底部表中的项目来重定向到另一个页面。
这是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);
}
这是我的RecyclerViewAdapter
的示例代码
private void openFragmentScreen(int position, View view) {
// This is where the recyclerview item click listener to redirect in another fragment
}
我已经做到了:
recyclerview.onClickListener
但它不起作用
有没有办法在适配器类中调用bottomsheetdialog,以便我可以在openFragmentScreen()
中轻松使用bottomsheet.dismiss()
?
最佳答案
问题不太清楚,但你有两种方法与适配器类交互
(这不是正确的方法,但在这种情况下有效)
只需将对话框的实例传递给适配器类的构造函数 并在 onbind 方法内部使用该对话框实例来关闭它
创建一个接口(interface),将其作为适配器类中的构造函数参数, 每当您的 Activity/fragment 收到通知时调用该函数, 然后,当您想要创建适配器类的实例时,让该类通过将“this”而不是该接口(interface)传递给您的适配器实例来实现该接口(interface)。
关于java - 当用户单击 RecyclerView 中的项目时如何关闭 BottomSheetDialog?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74231667/