Android: Open Dialog On Adapter
Solution 1:
This is what I normally do when dealing with recyclerView items click events:
Create a click listener to reuse later.
publicclassRecyclerItemClickListenerimplementsRecyclerView.OnItemTouchListener {
@SuppressWarnings("CanBeFinal")private OnItemClickListener mListener;
publicinterfaceOnItemClickListener {
voidonItemClick(View view, int position);
}
@SuppressWarnings("CanBeFinal")private GestureDetector mGestureDetector;
publicRecyclerItemClickListener(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = newGestureDetector(context, newGestureDetector.SimpleOnGestureListener() {
@OverridepublicbooleanonSingleTapUp(MotionEvent e) {
returntrue;
}
});
}
@OverridepublicbooleanonInterceptTouchEvent(RecyclerView view, MotionEvent e) {
ViewchildView= view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
}
returnfalse;
}
@OverridepublicvoidonTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}
@OverridepublicvoidonRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}}
Then to use it in your Activity, do this:
mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
showDialog();
}
}));
I hope this helps!
Solution 2:
Inside your RecyclerviewAdapter, and in the onClick method of the view (the view that clicked shows this dialog), u can place the below code. Edited:
MyDialogFragmentnewFragment= MyDialogFragment.newInstance();
newFragment.show(((Activity) context).getSupportFragmentManager(), "Title");
Note: the "context" is the context of the activity from where the recyclerview is created. (the context variable passed as an argument in the cnstructor of RecyclerviewAdapter.)
Note2: MyDialogFragment is a dialogfragment that u created. In this MyDialogFragment u should have the constructor newInstance()
The MyDialogFragment can look like this:
publicclassMyDialogFragmentextendsDialogFragment {
publicstatic MyDialogFragment newInstance() {
MyDialogFragmentfrag=newMyDialogFragment();
return frag;
}
@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builderbuilder=newAlertDialog.Builder(getActivity());
// Get the layout inflaterLayoutInflaterinflater= getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.fragment_myDialog, null))
.setTitle("Title")
...
// Set ur code return builder.create();
}
}
So now for how to pass the context, from the mainActivity, when u create the adapter n the activity context that u pass to the adapter is the context i m talking about. For Example: the "this" in the code below is the activity context that u will be passing to your adapter. This code is present in ur MainActivity.
MyAdapteradapter1=newMyAdapter(this, array_list);
mRecyclerView.setAdapter(adapter1);
Solution 3:
Set this on the DialogFragment:
publicstatic myFragment newInstance() {
returnnew myFragment();
}
Set this on the adapter:
public Activity mcontext;
publicSubjectsAdapter(Activity context){
// Here we're getting the activity's context, // by setting the adapter on the activity with (this) this.mcontext=context;
}
Show it in through the adapter like this:
myFragmentnewFragment= myFragment.newInstance();
newFragment.show(mcontext.getFragmentManager(), "Title");
Post a Comment for "Android: Open Dialog On Adapter"