gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 22:10:53 25 4
gpt4 key购买 nike

我将 ViewPager 作为 RecyclerView 中的第一项。我想将 FragmentStatePagerAdapter 设置为 viewpageronBindViewHolder() 方法中。但我不能调用 getsupportfragmentmanager()。请帮忙!!。这是我的代码:

public class CustomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<RowItem> rowItemList;
private final int VIEW_TYPE_CUSTOM = 0;
private final int VIEW_TYPE_NORMAL = 1;
NewPagerAdapter mCustomPagerAdapter;

public CustomListAdapter(Context context, List<RowItem> 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();

关于android - 如何从适配器类调用 getsupportfragmentmanager()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31961403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com