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

huangapple go评论106阅读模式
英文:

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:

                }
            }
        }
));

当在RecyclerView中只有两到五个项目时,此代码效果最佳。但如果RecyclerView中有数千个项目,那么如何使用onClickItemonLongItemClick呢?因为使用switch语句将导致最坏的情况。

英文:
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. 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.

答案1

得分: 1

在您的 RecyclerView 适配器中实现一个 接口

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

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

    // 构造函数中传入数据
    public ProductListAdapter(Context context, List<AccountTypeModel> data, ItemClickListener mClickListener) {
        this.mInflater = LayoutInflater.from(context);
        this.mData = data;
        this.context = context;
        this.mClickListener = mClickListener;
    }

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

    @Override
    public int getItemCount() {
        return mData.size();
    }

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

    public String getItem(int id) {
        return mData.get(id).getAccountID();
    }

    public void setClickListener(ItemClickListener itemClickListener) {
        this.mClickListener = itemClickListener;
    }

    public interface ItemClickListener {
        void onItemClick(View view, int position, String name);
    }
}

然后在您的 Activity 类中实现 onClick 方法。

英文:

implement an interface inside the adapter of you RecycleView

Something like:

public class ProductListAdapter extends RecyclerView.Adapter&lt;ProductListAdapter.ViewHolder&gt; {
private List&lt;AccountTypeModel&gt; mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
Context context;
// data is passed into the constructor
public ProductListAdapter(Context context, List&lt;AccountTypeModel&gt; 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.

huangapple
  • 本文由 发表于 2020年10月17日 23:46:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64404317.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定