简体   繁体   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:

                    }
                }
            }
    ));
}

This code is best while I have only two or up to 5 items in my recycler view.当我的回收站视图中只有两个或最多 5 个项目时,此代码是最好的。 But what happens if I was thousands of items inside my recycler view then how can I use the onClickItem or onLongItemClick, because using switch statement will be the worst case.但是如果我的回收器视图中有数千个项目会发生什么,那么我如何使用 onClickItem 或 onLongItemClick,因为使用 switch 语句将是最坏的情况。

implement an interface inside the adapter of you RecycleView在 RecycleView 的适配器内部实现一个接口

Something like:就像是:

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);
        }
}

Then Implement the onClick in your Activity class.然后在您的 Activity 类中实现onClick

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在项目上单击RecyclerView的监听器 - 如何在回收站视图中使用项目的位置来平滑滚动到另一个列表视图? - On Item Click Listener for RecyclerView - How can I use the position of an item in a recycler view to smoothscroll to another listview? 如何设置“项目点击侦听器”上的视图寻呼机? 设置点击监听器不起作用 - How can I set view pager's on Item Click Listener? Set On Click Listener is not working 当我单击另一个recyclerview中的项目时如何显示回收者视图 - How to show recycler view when i click an item in another recyclerview 我如何使用应用程序的每个回收站查看项目链接,以在链接点击上转到特定项目页面,例如flipkart购物应用程序 - How do I use app's each recycler view items links to go to particular item page on link click like flipkart shopping app 如何在回收站视图中只获取我想要的项目 - how can get only item that I wanted in recycler view 如何在每个回收站视图项目内单击按钮时实现文本计数器 - How to implement text counter on button click inside each recycler view item 定位在片段内的回收器视图项内的按钮侦听器 - Target a button listener that is inside a recycler view item that is inside a fragment 我有一个 Recycler View,onRun 会显示,但是单击按钮后它将是空的,我想设置一个默认的 recycler 视图 - I have a Recycler View, onRun will display, but upon a button click it will be empty, I want to set a default recycler view 从回收者视图中删除项目时,回收者视图未刷新吗? - Recycler view is not refreshed when i delete an item from recycler view? 带有 actionLayout 的 NavigationView 菜单项。 如何为每个项目设置这些动作布局的属性? - NavigationView menu items with actionLayout. How can I set those action layout's attribute for each item?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM