I have a dialog fragment in a recyclerview which works fine. The problem is i am not able to close it after the user has selected one of the options from the dialog fragment (The dialog fragment has another recyclerview populating it on whose item click i want to do something and close the dialog fragment). There are partial answers around but nothing complete.
Thanks For overview Fragment>RecyclerView>Dialog Fragment>RecyclerView with menu options
On click of menu options i want to close the dialog fragment.
Following is the adapter code where from i call the click event on the view:
public class ContextMenuAdapter extends RecyclerView.Adapter<ContextMenuAdapter.contextMenuViewHolder> {
ArrayList<ContextMenuModel> contextMenuList;
Context contextMenuContext;
public ContextMenuAdapter(ArrayList<ContextMenuModel> contextMenuList, Context contextMenuContext) {
this.contextMenuList = contextMenuList;
this.contextMenuContext = contextMenuContext;
}
@NonNull
@Override
public contextMenuViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View contextMenuView = LayoutInflater.from(contextMenuContext).inflate(R.layout.sample_context_menu_rv, parent, false);
return new contextMenuViewHolder(contextMenuView);
}
@Override
public void onBindViewHolder(@NonNull final contextMenuViewHolder holder, int position) {
ContextMenuModel accountFeedModelPosition = contextMenuList.get(position);
holder.tvContextMenuOption.setText(accountFeedModelPosition.getMenuOption());
// On Click Listener for the recycler view items.
// 1. On Click menu option
holder.tvContextMenuOption.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(contextMenuContext, "Clicked"+ holder.tvContextMenuOption.getText(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return contextMenuList.size();
}
public class contextMenuViewHolder extends RecyclerView.ViewHolder{
TextView tvContextMenuOption;
public contextMenuViewHolder(@NonNull View itemView) {
super(itemView);
tvContextMenuOption = itemView.findViewById(R.id.tvContextMenuOption);
}
}
}
Finally achieved it by following this post: How to dismiss a dialog fragment from a recyclerview adapter
Solution:
In the adapter.java add to the adapter a constructor that receive DialogFragment.
ArrayList contextMenuList; Context contextMenuContext; DialogFragment cmDialogFrag; //For this purpose
public ContextMenuAdapter(ArrayList contextMenuList , Context contextMenuContext, DialogFragment cmDialogFrag) { this.contextMenuList = contextMenuList; this.contextMenuContext = contextMenuContext; this.cmDialogFrag = cmDialogFrag; //For this purpose }
When you create the Adapter in the activity.java or fragment.java pass value for constructor created in step 1, like this:
ContextMenuAdapter myCMAdapter = new ContextMenuAdapter(contextMenuList , getActivity() , ContextMenuFragment.this);
Here the last one i.e. ContextMenuFragment.this will be passed to the constructor created in step 1.
Inside the onClick of recyclerview view call:
cmDialogFrag.dismiss();