Home » Android » android – doesn't show getSupportFragmentManager on ViewHolder in RecyclerView

android – doesn't show getSupportFragmentManager on ViewHolder in RecyclerView

Posted by: admin June 15, 2020 Leave a comment


In ViewHolder, I can’t call getSupportFragmentManager.

I want to change between Fragment

I searched all of the google page. but I can’t find it.

public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    public ImageView imgThumbnail;//img
    public TextView tvspecies; //text

    public ViewHolder(View itemView) {

        imgThumbnail = (ImageView)itemView.findViewById(R.id.img_thumbnail);
        tvspecies = (TextView)itemView.findViewById(R.id.tv_species);



    public void onClick(View v) {/*버튼 실행했을때 실행부분*/

        Fragment fm=new NewsFragment();
        FragmentTransaction transaction=fm.getFragmentManager().beginTransaction();
        transaction.replace(R.id.Linearcontainer, fm);


How to&Answers:

Try this one

public HorizontalProductAdapter(Activity context, List<RecycleIndividualProduct>data, FragmentManager fragmentManager){
        inflater = LayoutInflater.from(context);
        this.subActivityData = data;

and call your adapter from your activity or fragment

 mAdapter = new HorizontalProductAdapter(getActivity(),allDeals,getFragmentManager());


Just do this,
Pass getSupportFragmentManager from your activity or fragment where you instantiate your adapter, and do this in your adapter class constructor.

 public ViewHolder(View itemView, FragmentManager fragmentManager) {

        imgThumbnail = (ImageView)itemView.findViewById(R.id.img_thumbnail);
        tvspecies = (TextView)itemView.findViewById(R.id.tv_species);
        this.fragmentManager = fragmentManager



In my recyclerview adapter class I did this:

//recyclerView adapter global variable
private List<Data_Class> dataModel;
private Context mContext;
private FragmentManager FragManager;

//Recyclerview adapter constructor
public Recycler_Adapter_Class(Context appContext, List <Data_Class> data, FragmentManager fmanager) {
    this.mContext = appContext;
    this.dataModel = data;
    this.FragManager = fmanager;

//In the activity or Fragment that calls the recyclerview adapter I did this:

Recycler_Adapter_Class RC = new Recycler_Adapter_Class(this, Data_Class_List, this.getSupportFragmentManager());

RecyclerView rView = (RecyclerView) findViewById(R.id.recycler_view);


Hope this helps someone. Cheers!


<p>Pass FragmentTransaction by constructing and then replace.</p>  
<code>public SettingsAdapter(Context context, List<Settings> settingsList, android.support.v4.app.FragmentTransaction ft){
this.context = context;
        this.settingsList = settingsList;
        this.ft = ft;

// Calling the SettingsAdapter with constructors

    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        settingsAdapter = new SettingsAdapter(getActivity(), settingsList, fragmentTransaction);`enter code here`