片段中的 RecyclerView OnclickListener
RecyclerView OnclickListener in a fragment
我正在尝试在片段中的 recyclyerView 上设置点击侦听器我不知道如何在片段中调用 activity 我只知道在构造函数中解析上下文,但是 clickListener 没有这样做之前,我尝试过但它根本不起作用
public class RecView extends RecyclerView.Adapter<RecView .SuggestedViewHolder>
{
ArrayList<SuggestedModel> suggestedModels;
final private RecyclerViewClickListener clickListener;
public RecView(ArrayList<SuggestedModel> suggestedModels_items, RecyclerViewClickListener clickListener)
{
this.suggestedModels_items = suggestedModels_items;
this.clickListener = clickListener;
}
@Override
public void onBindViewHolder(@NonNull SuggestedViewHolder holder, int position)
{
SuggestedModel suggestedModel=suggestedModels.get(position);
holder.image.setImageResource(suggestedModel.getImage());
holder.title.setText(suggestedModel.getTitle());
}
@NonNull
@Override
public SuggestedViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_recview_suggested_item,parent,false);
final SuggestedViewHolder suggestedViewHolder = new SuggestedViewHolder(view);
return suggestedViewHolder;
}
@Override
public int getItemCount()
{
return suggestedModels.size();
}
public class SuggestedViewHolder extends RecyclerView.ViewHolder
{
CircleImageView image;
TextView title;
public SuggestedViewHolder(@NonNull View itemView)
{
super(itemView);
image = itemView.findViewById(R.id.suggestedImage);
title = itemView.findViewById(R.id.suggestedTitle);
}
}
//////////
this is what i called in the fragment
private void getSuggestedData()
{
rec_TopMost.setHasFixedSize(true);
rec_TopMost.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false));
suggestedModels.add(new SuggestedModel(R.drawable.s1,"Movie1"));
suggestedModels.add(new SuggestedModel(R.drawable.s2,"Movie2"));
suggestedModels.add(new SuggestedModel(R.drawable.s3,"Movie3"));
suggestedModels.add(new SuggestedModel(R.drawable.s4,"Movie4"));
suggestedModels.add(new SuggestedModel(R.drawable.s5,"Movie5"));
suggestedModels.add(new SuggestedModel(R.drawable.s6,"Movie6"));
suggestedModels.add(new SuggestedModel(R.drawable.s7,"Movie7"));
suggestedModels.add(new SuggestedModel(R.drawable.s8,"Movie8"));
suggestedModels.add(new SuggestedModel(R.drawable.s9,"Movie9"));
RecView= new RecView( suggestedModel,(RecyclerViewClickListener) getContext());
rec_TopMost.setAdapter(RecView);
}
我该怎么做我试了很多东西都行不通
{
RecView= new RecView( suggestedModels, getContext());
rec_TopMost.setAdapter(RecView);
}
如果我这样做,它会显示红线。在 getContext();
下
这不是 Activity
或 Fragment
的问题,您的 RecView
将 RecyclerViewClickListener
作为第二个参数,让您的 Fragment
实现此接口就像Activity
确实
我正在尝试在片段中的 recyclyerView 上设置点击侦听器我不知道如何在片段中调用 activity 我只知道在构造函数中解析上下文,但是 clickListener 没有这样做之前,我尝试过但它根本不起作用
public class RecView extends RecyclerView.Adapter<RecView .SuggestedViewHolder>
{
ArrayList<SuggestedModel> suggestedModels;
final private RecyclerViewClickListener clickListener;
public RecView(ArrayList<SuggestedModel> suggestedModels_items, RecyclerViewClickListener clickListener)
{
this.suggestedModels_items = suggestedModels_items;
this.clickListener = clickListener;
}
@Override
public void onBindViewHolder(@NonNull SuggestedViewHolder holder, int position)
{
SuggestedModel suggestedModel=suggestedModels.get(position);
holder.image.setImageResource(suggestedModel.getImage());
holder.title.setText(suggestedModel.getTitle());
}
@NonNull
@Override
public SuggestedViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_recview_suggested_item,parent,false);
final SuggestedViewHolder suggestedViewHolder = new SuggestedViewHolder(view);
return suggestedViewHolder;
}
@Override
public int getItemCount()
{
return suggestedModels.size();
}
public class SuggestedViewHolder extends RecyclerView.ViewHolder
{
CircleImageView image;
TextView title;
public SuggestedViewHolder(@NonNull View itemView)
{
super(itemView);
image = itemView.findViewById(R.id.suggestedImage);
title = itemView.findViewById(R.id.suggestedTitle);
}
}
//////////
this is what i called in the fragment
private void getSuggestedData()
{
rec_TopMost.setHasFixedSize(true);
rec_TopMost.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false));
suggestedModels.add(new SuggestedModel(R.drawable.s1,"Movie1"));
suggestedModels.add(new SuggestedModel(R.drawable.s2,"Movie2"));
suggestedModels.add(new SuggestedModel(R.drawable.s3,"Movie3"));
suggestedModels.add(new SuggestedModel(R.drawable.s4,"Movie4"));
suggestedModels.add(new SuggestedModel(R.drawable.s5,"Movie5"));
suggestedModels.add(new SuggestedModel(R.drawable.s6,"Movie6"));
suggestedModels.add(new SuggestedModel(R.drawable.s7,"Movie7"));
suggestedModels.add(new SuggestedModel(R.drawable.s8,"Movie8"));
suggestedModels.add(new SuggestedModel(R.drawable.s9,"Movie9"));
RecView= new RecView( suggestedModel,(RecyclerViewClickListener) getContext());
rec_TopMost.setAdapter(RecView);
}
我该怎么做我试了很多东西都行不通
{
RecView= new RecView( suggestedModels, getContext());
rec_TopMost.setAdapter(RecView);
}
如果我这样做,它会显示红线。在 getContext();
下这不是 Activity
或 Fragment
的问题,您的 RecView
将 RecyclerViewClickListener
作为第二个参数,让您的 Fragment
实现此接口就像Activity
确实