片段中的 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】:

ActivityFragment不是问题,你的RecViewRecyclerViewClickListener作为第二个参数,让你的Fragment实现这个接口就像Activity一样

【讨论】:

以上是关于片段中的 RecyclerView OnclickListener的主要内容,如果未能解决你的问题,请参考以下文章

从其他片段添加新的 RecyclerView 项

处理recyclerview单击片段而不是持有者类

RecyclerView onClickListener 适用于所有片段

视图重新添加到视图寻呼机中的片段

更改片段(子片段)时 RecyclerView 中的奇怪叠加

更新片段中的 RecyclerView