作者热门文章
- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
标签布局
在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/
所以我的问题出现在库 fragment 中的上下文中。我收到以下错误。 CardAdapter(Android.content.Context, in ArrayList in CardAdapter
标签布局 tab1( fragment 1) tab2( fragment 2) tab3( fragment 3) RecyclerView + CardView (OnClick) 在CardVi
我是一名优秀的程序员,十分优秀!