如何从适配器类调用getsupportfragmentmanager()?

我将ViewPager作为RecyclerView的第一个项目。我想将FragmentStatePagerAdapter设置为onBindViewHolder()方法中的viewpager 。 但我不能打电话给getsupportfragmentmanager() 。请帮助!! 这是我的代码:

 public class CustomListAdapter extends RecyclerView.Adapter { private Context context; private List rowItemList; private final int VIEW_TYPE_CUSTOM = 0; private final int VIEW_TYPE_NORMAL = 1; NewPagerAdapter mCustomPagerAdapter; public CustomListAdapter(Context context, List rowItemList) { this.context = context; this.rowItemList = rowItemList; } @Override public int getItemViewType(int position) { if (position == 0) return VIEW_TYPE_CUSTOM; else return VIEW_TYPE_NORMAL; } @Override public int getItemCount() { return rowItemList.size()+1; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { switch (getItemViewType(position)) { case VIEW_TYPE_CUSTOM: recViewHolder1 viewHolderSecond = (recViewHolder1) holder; mCustomPagerAdapter = new NewPagerAdapter(getSupportFragmentManager()); viewHolderSecond.vPager.setAdapter(mCustomPagerAdapter); break; case VIEW_TYPE_NORMAL: recViewHolder2 viewHolderFirst = (recViewHolder2) holder; RowItem rowItem = rowItemList.get(position-1); viewHolderFirst.vName.setText(rowItem.getName()); viewHolderFirst.vImage.setImageResource(rowItem.getImageId()); break; } } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { switch (viewType) { case VIEW_TYPE_CUSTOM: View itemView = LayoutInflater. from(parent.getContext()). inflate(R.layout.pagerlayout, parent, false); return new recViewHolder1(itemView); case VIEW_TYPE_NORMAL: View itemView1 = LayoutInflater. from(parent.getContext()). inflate(R.layout.rowlayout, parent, false); return new recViewHolder2(itemView1); } return null; } @Override public long getItemId(int position) { return super.getItemId(position); } public static class recViewHolder1 extends RecyclerView.ViewHolder { protected static ViewPager vPager; public recViewHolder1(View v) { super(v); vPager = (ViewPager) v.findViewById(R.id.vPager); } } public class recViewHolder2 extends RecyclerView.ViewHolder implements View.OnClickListener { protected TextView vName; protected ImageView vImage; public recViewHolder2(View v) { super(v); v.setOnClickListener(this); vName = (TextView) v.findViewById(R.id.vName); vImage = (ImageView) v.findViewById(R.id.vImage); } } } 

我通过转换mContext来解决它

 ((FragmentActivity)mContext).getSupportFragmentManager().beginTransaction() .replace(R.id.item_detail_container, fragment) .commit(); 

在这里你可以设置转换;)

 public void pushFragment(Fragment newFragment, Context context){ FragmentTransaction transaction = ((FragmentActivity)context).getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.fragment, newFragment); transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); transaction.addToBackStack(null); transaction.commit(); } 

首先,您必须将上下文传递给适配器构造函数。

然后,您必须使用该上下文引用来获取getSupportFragmentManager

 mCustomPagerAdapter = new NewPagerAdapter(context.getSupportFragmentManager()); viewHolderSecond.vPager.setAdapter(mCustomPagerAdapter); 

另一种方案……

声明上下文

 private Toast mssgeToast; private Context ctxContext; public MyViewListener(myView pView){ this.mView = pView; this.ctxContext = pView.getContext(); } 

下一个…

  public void pushFragment(Context context){ FragmentManager fm = ((FragmentActivity)context).getSupportFragmentManager(); PopupClickFragment pcf = new PopupClickFragment(); pcf.show(fm, ""); // FragmentTransaction transaction = ((FragmentActivity)context).getSupportFragmentManager().beginTransaction(); } 

并声明为onClickListener。 例:

  @Override public void onCellClicked (@NonNull RecyclerView.ViewHolder p_jCellView,int p_nXPosition, int p_nYPosition){ //TODO: AQUI SE DEBE LEVANTAR EL POPUP INSTANCIANDO .SHOW(); pushFragment(ctxContext); } 

对于使用kotlin的人来说,你可以使用这个解决方案

 val dialog = IntervAddFragment() val ft = (context as AppCompatActivity).supportFragmentManager.beginTransaction() dialog.show(ft, ContentValues.TAG) 

并且它将完美地工作。

它的最佳解决方案是将您的Context的引用从您的Activity(或)Fragment传递给Adapter。

  public UsersAdapter(ArrayList items , Context context) { usersList = items; this.context = context; } 

然后像这样向AppCompatActivity上传上下文并获取getSupportFragmentManager

  ((AppCompatActivity) context).getSupportFragmentManager() 

分享改进。