Android recycler view onItemClick 识别适配器

Posted

技术标签:

【中文标题】Android recycler view onItemClick 识别适配器【英文标题】:Android recycler view onItemClick identify the adapter 【发布时间】:2020-10-31 04:18:40 【问题描述】:

我有 3 个不同的适配器用于 1 个 Recycler 视图,并且我已经在我的班级中实现了所有 3 个的 OnClick 侦听器:

    public class PokemonMovesDialogFragment extends DialogFragment
            implements LearnsetRecyclerAdapter.ItemClickListener,
            TecnicalHiddenMachineRecyclerAdapter.ItemClickListener,
            EggmoveRecyclerAdapter.ItemClickListener 

 private LearnsetRecyclerAdapter learnsetRecyclerAdapter;
        private TecnicalHiddenMachineRecyclerAdapter tecnicalHiddenMachineRecyclerAdapter;
        private EggmoveRecyclerAdapter eggmoveRecyclerAdapter;
           
        @Override
        public void onItemClick(View view, int position) 
           //HERE I NEED TO IDENTIFY WHICH ADAPTER I'VE SUMMONED
        
    
    
    
    public void setAdaptersListeners()
     //Here I instance the adapters and i set the listener...
    

我的 3 个适配器实现了这个:

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener 

   ......

    ViewHolder(View view) 
        super(view);

        ......
        view.setOnClickListener(this);
    

    @Override
    public void onClick(View view) 
        if (mClickListener != null) 
            mClickListener.onItemClick(view, getAdapterPosition());
        
    


// convenience method for getting data at click position
public Move getItem(int id) 
    return learnsetMoves.get(id);


// allows clicks events to be caught
public void setClickListener(ItemClickListener itemClickListener) 
    this.mClickListener = itemClickListener;


// parent activity will implement this method to respond to click events
public interface ItemClickListener 
    void onItemClick(View view, int position);

我已经对其进行了测试及其工作,使用调试器我可以看到单击时调用了哪个位置和视图,但是我不知道调用了哪些适配器,我只得到了视图和位置:

【问题讨论】:

【参考方案1】:
public enum AdapterType 
    First,
    Second,
    Third


@Override
public void onClick(View view) 
    if (mClickListener != null) 
        view.setTag(AdapterType.First);
        mClickListener.onItemClick(view, getAdapterPosition());
    


@Override
public void onItemClick(View view, int position) 
    //HERE I NEED TO IDENTIFY WHICH ADAPTER I'VE SUMMONED
    AdapterType adapterType = (AdapterType) view.getTag();

【讨论】:

以上是关于Android recycler view onItemClick 识别适配器的主要内容,如果未能解决你的问题,请参考以下文章

我想使用 GeoFire 在 android 中填充 Firebase Recycler View

谁能建议如何在android Recycler View中按产品价格实现排序[关闭]

在 Android 的 Recycler View 中生成和设置文本视图背景的随机颜色

我的应用程序的Recycler View问题中的Android Studio SearchView过滤器

Recycler View重复,如何查看一次

在 Recycler View 中,从 LinearLayoutManger 转换为 GridLayoutManager 没有区别