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

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

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

问题

  1. recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(
  2. this,
  3. recyclerView,
  4. new RecyclerItemClickListener.OnItemClickListener() {
  5. @Override
  6. public void onItemClick(View view, int position) {
  7. switch (position){
  8. case 0:
  9. Toast.makeText(MainActivity.this, "item-1 Clicked", Toast.LENGTH_SHORT).show();
  10. break;
  11. case 1:
  12. Toast.makeText(MainActivity.this, "item-2 Clicked", Toast.LENGTH_SHORT).show();
  13. break;
  14. default:
  15. }
  16. }
  17. @Override
  18. public void onLongItemClick(View view, int position) {
  19. switch (position){
  20. case 0:
  21. Toast.makeText(MainActivity.this, "Get 10% Discount", Toast.LENGTH_SHORT).show();
  22. break;
  23. case 1:
  24. Toast.makeText(MainActivity.this, "Get 20% Discount", Toast.LENGTH_SHORT).show();
  25. break;
  26. default:
  27. }
  28. }
  29. }
  30. ));

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

英文:
  1. recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(
  2. this,
  3. recyclerView,
  4. new RecyclerItemClickListener.OnItemClickListener() {
  5. @Override
  6. public void onItemClick(View view, int position) {
  7. switch (position){
  8. case 0:
  9. Toast.makeText(MainActivity.this, "item-1 Clicked", Toast.LENGTH_SHORT).show();
  10. break;
  11. case 1:
  12. Toast.makeText(MainActivity.this, "item-2 Clicked", Toast.LENGTH_SHORT).show();
  13. break;
  14. default:
  15. }
  16. }
  17. @Override
  18. public void onLongItemClick(View view, int position) {
  19. switch (position){
  20. case 0:
  21. Toast.makeText(MainActivity.this, "Get 10% Discount", Toast.LENGTH_SHORT).show();
  22. break;
  23. case 1:
  24. Toast.makeText(MainActivity.this, "Get 20% Discount", Toast.LENGTH_SHORT).show();
  25. break;
  26. default:
  27. }
  28. }
  29. }
  30. ));
  31. }

> 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 适配器中实现一个 接口

  1. public class ProductListAdapter extends RecyclerView.Adapter<ProductListAdapter.ViewHolder> {
  2. private List<AccountTypeModel> mData;
  3. private LayoutInflater mInflater;
  4. private ItemClickListener mClickListener;
  5. Context context;
  6. // 构造函数中传入数据
  7. public ProductListAdapter(Context context, List<AccountTypeModel> data, ItemClickListener mClickListener) {
  8. this.mInflater = LayoutInflater.from(context);
  9. this.mData = data;
  10. this.context = context;
  11. this.mClickListener = mClickListener;
  12. }
  13. @Override
  14. public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  15. View view = mInflater.inflate(R.layout.product_list_item_design, parent, false);
  16. return new ViewHolder(view);
  17. }
  18. @Override
  19. public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
  20. AccountTypeModel currentItem = mData.get(position);
  21. holder.txtproductname.setText(currentItem.getAccountName());
  22. }
  23. @Override
  24. public int getItemCount() {
  25. return mData.size();
  26. }
  27. public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
  28. TextView txtproductname;
  29. ConstraintLayout relativeLayout;
  30. ViewHolder(View itemView) {
  31. super(itemView);
  32. itemView.setOnClickListener(this);
  33. txtproductname = itemView.findViewById(R.id.txtproductname);
  34. relativeLayout = itemView.findViewById(R.id.myItemLayout);
  35. }
  36. @Override
  37. public void onClick(View view) {
  38. if (mClickListener != null) {
  39. mClickListener.onItemClick(view, getAdapterPosition(), mData.get(getAdapterPosition()).getAccountID());
  40. }
  41. notifyDataSetChanged();
  42. }
  43. }
  44. public String getItem(int id) {
  45. return mData.get(id).getAccountID();
  46. }
  47. public void setClickListener(ItemClickListener itemClickListener) {
  48. this.mClickListener = itemClickListener;
  49. }
  50. public interface ItemClickListener {
  51. void onItemClick(View view, int position, String name);
  52. }
  53. }

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

英文:

implement an interface inside the adapter of you RecycleView

Something like:

  1. public class ProductListAdapter extends RecyclerView.Adapter&lt;ProductListAdapter.ViewHolder&gt; {
  2. private List&lt;AccountTypeModel&gt; mData;
  3. private LayoutInflater mInflater;
  4. private ItemClickListener mClickListener;
  5. Context context;
  6. // data is passed into the constructor
  7. public ProductListAdapter(Context context, List&lt;AccountTypeModel&gt; data, ItemClickListener mClickListener) {
  8. this.mInflater = LayoutInflater.from(context);
  9. this.mData = data;
  10. this.context = context;
  11. this.mClickListener = mClickListener;
  12. }
  13. // inflates the row layout from xml when needed
  14. @Override
  15. public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  16. View view = mInflater.inflate(R.layout.product_list_item_design, parent, false);
  17. return new ViewHolder(view);
  18. }
  19. @Override
  20. public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
  21. AccountTypeModel currentItem = mData.get(position);
  22. holder.txtproductname.setText(currentItem.getAccountName());
  23. }
  24. // binds the data to the TextView in each row
  25. // total number of rows
  26. @Override
  27. public int getItemCount() {
  28. return mData.size();
  29. }
  30. // stores and recycles views as they are scrolled off screen
  31. public class ViewHolder extends `RecyclerView.ViewHolder implements View.OnClickListener` {
  32. TextView txtproductname;
  33. ConstraintLayout relativeLayout;
  34. ViewHolder(View itemView) {
  35. super(itemView);
  36. itemView.setOnClickListener(this);
  37. txtproductname = itemView.findViewById(R.id.txtproductname);
  38. relativeLayout = itemView.findViewById(R.id.myItemLayout);
  39. }
  40. @Override
  41. public void onClick(View view) {
  42. if (mClickListener != null) {
  43. mClickListener.onItemClick(view, getAdapterPosition(), mData.get(getAdapterPosition()).getAccountID());
  44. }
  45. notifyDataSetChanged();
  46. }
  47. }
  48. // convenience method for getting data at click position
  49. public String getItem(int id) {
  50. return mData.get(id).getAccountID();
  51. }
  52. // allows clicks events to be caught
  53. public void setClickListener(ItemClickListener itemClickListener) {
  54. this.mClickListener = itemClickListener;
  55. }
  56. // parent activity will implement this method to respond to click events
  57. public interface ItemClickListener {
  58. void onItemClick(View view, int position, String name);
  59. }
  60. }

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:

确定