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) {
super(itemView);
imgThumbnail = (ImageView)itemView.findViewById(R.id.img_thumbnail);
tvspecies = (TextView)itemView.findViewById(R.id.tv_species);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {/*버튼 실행했을때 실행부분*/
Fragment fm=new NewsFragment();
FragmentTransaction transaction=fm.getFragmentManager().beginTransaction();
transaction.replace(R.id.Linearcontainer, fm);
transaction.addToBackStack(null);
transaction.commit();
}//onClick
}//ViewHolder
Try this one
public HorizontalProductAdapter(Activity context, List<RecycleIndividualProduct>data, FragmentManager fragmentManager){
inflater = LayoutInflater.from(context);
this.subActivityData = data;
this.fragmentManager=fragmentManager;
}
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) {
super(itemView);
imgThumbnail = (ImageView)itemView.findViewById(R.id.img_thumbnail);
tvspecies = (TextView)itemView.findViewById(R.id.tv_species);
this.fragmentManager = fragmentManager
itemView.setOnClickListener(this);
}
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);
rView.setAdapter(RC);
Hope this helps someone. Cheers!