簡體   English   中英

如果我在回收器視圖中有數千個項目,那么如何在每個項目上設置項目點擊偵聽器

[英]If I Have thousands of items inside the recycler view then How can I set the item click listener on each item

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 語句將是最壞的情況。

在 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM