2015-08-25 17 views
8

Trong ViewHolder, tôi không thể gọi getSupportFragmentManager.không hiển thị getSupportFragmentManager trên ViewHolder trong RecyclerView

Tôi muốn thay đổi giữa Fragment

Tôi đã tìm kiếm tất cả trang google. nhưng tôi không thể tìm thấy nó.

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 
+0

bạn đã tìm ra câu trả lời? –

Trả lời

0

Chỉ cần làm điều này, đèo getSupportFragmentManager từ hoạt động hoặc mảnh nơi bạn nhanh chóng chuyển đổi của bạn của bạn, và làm điều này trong constructor lớp adapter của bạn.

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

    } 
13

Hãy thử điều này một

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

và gọi adapter của bạn từ hoạt động hoặc mảnh

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

của bạn Trong lớp recyclerview bộ chuyển đổi của tôi, tôi đã làm điều này:

//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 điều này giúp ai đó. Chúc mừng!

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`

Các vấn đề liên quan