Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call getFragmentManager on Recycler.Adapter?

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.

like image 219
SomeDevloperName Avatar asked Jun 16 '15 11:06

SomeDevloperName


People also ask

How do I contact Getfragmentmanager in adapter?

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.

What does recycler adapter do?

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.

What is the purpose of the adapter class used with recycler view?

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 .


3 Answers

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(); } 
like image 63
natario Avatar answered Oct 03 '22 04:10

natario


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()
like image 44
Isaac Martin Otim Avatar answered Oct 03 '22 04:10

Isaac Martin Otim


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();
like image 32
Abhishek Sengupta Avatar answered Oct 03 '22 05:10

Abhishek Sengupta