如果我在回收站视图中有数千个项目,那么如何在每个项目上设置项目单击侦听器

Posted

技术标签:

【中文标题】如果我在回收站视图中有数千个项目,那么如何在每个项目上设置项目单击侦听器【英文标题】:If I Have thousands of items inside the recycler view then How can I set the item click listener on each item 【发布时间】:2021-01-31 20:53:45 【问题描述】:
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(
            this,
            recyclerView,
            new RecyclerItemClickListener.OnItemClickListener() 
                @Override
                public void onItemClick(View view, int position) 
                    switch (position)
                        case 0:
                            Toast.makeText(MainActivity.this, "item-1 Clicked", Toast.LENGTH_SHORT).show();
                            break;
                        case 1:
                            Toast.makeText(MainActivity.this, "item-2 Clicked", Toast.LENGTH_SHORT).show();
                            break;
                        default:

                    
                

                @Override
                public void onLongItemClick(View view, int position) 
                    switch (position)
                        case 0:
                            Toast.makeText(MainActivity.this, "Get 10% Discount", Toast.LENGTH_SHORT).show();
                            break;
                        case 1:
                            Toast.makeText(MainActivity.this, "Get 20% Discount", Toast.LENGTH_SHORT).show();
                            break;
                        default:

                    
                
            
    ));

当我的回收站视图中只有两个或最多 5 个项目时,此代码是最好的。但是,如果我的回收站视图中有数千个项目,那么我该如何使用 onClickItem 或 onLongItemClick,因为使用 switch 语句将是最坏的情况。

【问题讨论】:

【参考方案1】:

在你的 RecycleView 的适配器中实现一个接口

类似:

public class ProductListAdapter extends RecyclerView.Adapter<ProductListAdapter.ViewHolder> 

    private List<AccountTypeModel> mData;
    private LayoutInflater mInflater;
    private ItemClickListener mClickListener;
    Context context;


    // data is passed into the constructor
   public ProductListAdapter(Context context, List<AccountTypeModel> data, ItemClickListener mClickListener) 
        this.mInflater = LayoutInflater.from(context);
        this.mData = data;
        this.context = context;
        this.mClickListener = mClickListener;
    

    // inflates the row layout from xml when needed
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
        View view = mInflater.inflate(R.layout.product_list_item_design, parent, false);
        return new ViewHolder(view);
    

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) 

       AccountTypeModel currentItem = mData.get(position);
        holder.txtproductname.setText(currentItem.getAccountName());


    

    // binds the data to the TextView in each row

    // total number of rows
    @Override
    public int getItemCount() 
        return mData.size();
    


    // stores and recycles views as they are scrolled off screen
    public class ViewHolder extends `RecyclerView.ViewHolder implements View.OnClickListener` 
        TextView txtproductname;
        ConstraintLayout relativeLayout;

        ViewHolder(View itemView) 
            super(itemView);
            itemView.setOnClickListener(this);
            txtproductname = itemView.findViewById(R.id.txtproductname);
            relativeLayout = itemView.findViewById(R.id.myItemLayout);

        


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

        
    
    // convenience method for getting data at click position
    public String getItem(int id) 
        return mData.get(id).getAccountID();
    
    // 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, String name);
        

然后在您的 Activity 类中实现 onClick

【讨论】:

以上是关于如果我在回收站视图中有数千个项目,那么如何在每个项目上设置项目单击侦听器的主要内容,如果未能解决你的问题,请参考以下文章

如何从适配器刷新主要活动中的视图?

如何在回收站视图中的每个项目下方创建寻呼机指示器

如何在recyclerview不同的项目点击上打开几个不同的片段?

如何为我在回收站视图中的卡片视图中的每个项目创建一个点击监听器

如何在回收站视图中有多个视图

如何根据 UICollectionView 中有多少单元格来更改视图的大小