I am converting ListView of my app to RecyclerView. On ListView, it was very easy to implement OnClickListener but in RecyclerView, we have to do it in adapter. I want to open a new Fragment when user clicks on a item. To do this I have to call FragmentManager in adapter which I am not able to do. This is my code of RecyclerAdapter:
public ListItemViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
description = (TextView) itemView.findViewById(R.id.description);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//Call FragmentManager and add Fragment to it.
}
}
So, how to call FragmentManager and add Fragments in it. Is there any better way than this like sendingBroadcast or any other method.
You just need an activity context passed in your constructor. Be sure to call new Adapter(this,...) from activities and new Adapter(getActivity(),...) from fragments. Show activity on this post.
RecyclerView: What it is? As per Android documentation: RecyclerView is a UI component which allows us to create a scrolling list. It is basically a new ViewGroup used to render any adapter-based view in horizontal/vertical /grid or staggered grid manner using the Viewholder pattern.
RecyclerView. Adapter base class for presenting List data in a RecyclerView , including computing diffs between Lists on a background thread. Adapters provide a binding from an app-specific data set to views that are displayed within a RecyclerView .
You just need an activity context passed in your constructor. Be sure to call new Adapter(this,...)
from activities and new Adapter(getActivity(),...)
from fragments.
private Context context; @Override public void onClick(View v) { FragmentManager manager = ((AppCompatActivity)context).getSupportFragmentManager(); }
To add on to the approved answer: if you still get an error you might need to replace this line;
FragmentManager manager = ((Activity)context).getFragmentManager();
With this
FragmentManager manager = ((AppCompatActivity)context).getSupportFragmentManager();
For me this was because I was using the support.v4.app.FragmentManager
instead of the regular fragmentmanager
Still get an error?
As one comment below pointed out, this might throw a java.lang.ClassCastException:
and log ... cannot be cast to android.support.v7.app.AppCompatActivity
(check comments for details)
Their fix was to use this instead (I haven't tested it but it worked for them):
((AppCompatActivity)activity).getSupportFragmentManager()
Make sure to pass context to the ArrayAdapter or RecyclerViewAdpater,So that we can get it inside Adapter Class.
If your mainActivity is extending Activity then use :
FragmentManager fragmentManager = ((Activity)context).getFragmentManager();
If your mainActivity is extending AppCompatActivity then use :
FragmentManager fragmentManager = ((AppCompatActivity)context).getSupportFragmentManager();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With