gpt4 book ai didi

android - 如何从 RecyclerView.Adapter 打开 fragment

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

  1. 标签布局

    • tab1( fragment 1)
    • tab2( fragment 2)
    • tab3( fragment 3)
      • RecyclerView + CardView (OnClick)

CardView ClickListner 在tab3 中打开另一个 fragment 。那么如何在tab3中打开fragment呢。

错误在 getFragmentManager():

FragmentTransaction transaction = getFragmentManager().beginTransaction();

给了

/Adapter/CardAdapter.java Error:cannot find symbol method getSupportFragmentManager()

而不是这个,我尝试了:

FragmentTransaction transaction = activity.getFragmentManager().beginTransaction();
FragmentTransaction transaction = itemview.getContext().getFragmentManager().beginTransaction();

但是错误没有解决。

这是我的代码:

 public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {

List<NatureItem> mItems;
private int lastPosition = -1;
Context context;
TaskFragment main;
public CardAdapter(Context context,TaskFragment ma)
{

this.context=context;
main=ma;
}


public CardAdapter() {
super();
mItems = new ArrayList<NatureItem>();
NatureItem nature = new NatureItem();
nature.setName("The Paris Attack 2015");
nature.setDes("Lorem ipsum dolor sit amet.");
nature.setThumbnail(R.drawable.news1);
mItems.add(nature);

}



@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.custom_list, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);

return viewHolder;
}


@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
NatureItem nature = mItems.get(i);
viewHolder.tvNature.setText(nature.getName());
viewHolder.tvDesNature.setText(nature.getDes());
viewHolder.imgThumbnail.setImageResource(nature.getThumbnail());
// setAnimation(viewHolder.card,i);
}


@Override
public int getItemCount() {
return mItems.size();
}

class ViewHolder extends RecyclerView.ViewHolder {

private int lastPosition = -1;
public ImageView imgThumbnail;
public TextView tvNature;
public TextView tvDesNature;
// Button btnclear,btncancle;
CardView card;
Activity activity;
Context co;
public ViewHolder(final View itemView) {
super(itemView);
imgThumbnail = (ImageView) itemView.findViewById(R.id.img_thumbnail);
tvNature = (TextView) itemView.findViewById(R.id.tv_nature);
tvDesNature = (TextView) itemView.findViewById(R.id.tv_des_nature);
card = (CardView) itemView.findViewById(R.id.card);



card.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Toast.makeText(itemView.getContext(), "Clicked Card...", Toast.LENGTH_LONG).show();

ShareFragment newFragment = new ShareFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.viewFragments, newFragment);
transaction.addToBackStack(null);
transaction.commit();

}
});

}
}
}

最佳答案

在您的 onclick 中按如下方式打开新 fragment

@Override
public void onClick(View view){
AppCompatActivity activity = (AppCompatActivity) view.getContext();
Fragment myFragment = new MyFragment();
activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, myFragment).addToBackStack(null).commit();
}

关于android - 如何从 RecyclerView.Adapter<CardAdapter.ViewHolder> 打开 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34310592/

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