不在RecyclerView中的ViewHolder上显示getSupportFragmentManager

时间:2015-08-25 12:15:55

标签: fragment android-recyclerview

ViewHolder,我无法拨打getSupportFragmentManager

我想在片段之间进行更改

我搜索了所有的谷歌页面。但是我找不到它。

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

4 个答案:

答案 0 :(得分:14)

试试这个

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

并从您的活动或片段中调用您的适配器

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

答案 1 :(得分:0)

这样做, 从您实例化适配器的活动或片段传递getSupportFragmentManager,并在适配器类构造函数中执行此操作。

 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);

    }

答案 2 :(得分:0)

在我的recyclerview适配器类中,我这样做了:

//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);

希望这有助于某人。干杯!

答案 3 :(得分:0)

<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;
 }</code>

// Calling the SettingsAdapter with constructors

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