片段中的 RecyclerView OnclickListener
Posted
技术标签:
【中文标题】片段中的 RecyclerView OnclickListener【英文标题】:RecyclerView OnclickListener in a fragment 【发布时间】:2021-12-21 03:28:09 【问题描述】:我正在尝试在片段中的 recyclyerView 上设置点击侦听器,我不知道如何调用片段中的活动,我所知道的是解析构造函数中的上下文,但 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()下;
【问题讨论】:
首先你应该学习如何在项目中使用Recyclerview @Yaqoob 问题不在于RecyclerView
/adapter 实现,代码在Java中,也标记presnet,所以你的建议和链接是不相关的
回收站视图有什么问题?它在没有 cliclistener 的情况下工作正常,我是初学者,我以前没有使用过这个:/
【参考方案1】:
Activity
或Fragment
不是问题,你的RecView
将RecyclerViewClickListener
作为第二个参数,让你的Fragment
实现这个接口就像Activity
一样
【讨论】:
以上是关于片段中的 RecyclerView OnclickListener的主要内容,如果未能解决你的问题,请参考以下文章
RecyclerView onClickListener 适用于所有片段