如何在recyclerview android中的single_row_item的每个视图(imageView,textView)上添加不同的点击事件?
Posted
技术标签:
【中文标题】如何在recyclerview android中的single_row_item的每个视图(imageView,textView)上添加不同的点击事件?【英文标题】:how to add different click events on each view (imageView, textView) of single_row_item in a recyclerview android? 【发布时间】:2021-10-07 05:28:11 【问题描述】:here are my recyclerview and viewholder classes
public class BookAdapter extends RecyclerView.Adapter<BookAdapter.MyViewHolder>
private ArrayList<Book> bookList;
private OnItemClickListener itemClickListener;
public BookAdapter( ArrayList<Book> bookList, OnItemClickListener itemClickListener)
this.bookList = bookList;
this.itemClickListener = itemClickListener;
@Override
public BookAdapter.MyViewHolder onCreateViewHolder( ViewGroup parent, int viewType)
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_list_item,parent,false);
return new MyViewHolder(view);
@Override
public void onBindViewHolder( BookAdapter.MyViewHolder holder, int position)
Book book = bookList.get(position);
holder.book_imageview.setImageResource(book.getBookImageId());
holder.title_textview.setText(book.getBookTitle());
holder.author_textview.setText(book.getBookAuthor());
@Override
public int getItemCount()
return bookList.size();
public class MyViewHolder extends RecyclerView.ViewHolder
ImageView book_imageview;
TextView author_textview, title_textview;
public MyViewHolder(View itemView)
super(itemView);
book_imageview = itemView.findViewById(R.id.book_imageview);
author_textview = itemView.findViewById(R.id.author_textview);
title_textview = itemView.findViewById(R.id.title_textview);
// if i clcik on title then in toast title should only be displayed
// if i click on auther then in toast auther should only be displayed
switch (getAbsoluteAdapterPosition())
case R.id.title_textview:
title_textview.setOnClickListener(v ->itemClickListener.onClick(bookList.get(getLayoutPosition())));
break;
case R.id.author_textview:
author_textview.setOnClickListener(v ->itemClickListener.onClick(bookList.get(getLayoutPosition())));
我想问我如何在 itemView 的每个组件上添加点击列表器,例如 title_textview、author_textview 等,因为我无法在我的界面的实现方法中创建 switch 语句
这是我的主要活动我认为的主要问题在哪里。我想使用 Switch 语句通过单独单击 ROW-ITEM 的组件来获取单个结果
public class MainActivity extends AppCompatActivity implements OnItemClickListener
private ArrayList<Book> bookList;
private RecyclerView rv;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bookList = new ArrayList<>();
Resources res = getResources();
String[] allBooks = res.getStringArray(R.array.books);
String[] allAuthors = res.getStringArray(R.array.authors);
pupoluateBookList(allBooks, allAuthors);
BookAdapter adapter = new BookAdapter(bookList, this);
rv = findViewById(R.id.rv);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
rv.setLayoutManager(linearLayoutManager);
rv.setAdapter(adapter);
private void showToast(String message)
Toast.makeText(this,message,Toast.LENGTH_LONG).show();
public void pupoluateBookList(String[] books, String[] authors)
bookList.add(new Book(books[0], authors[0], R.drawable.davinci_code));
bookList.add(new Book(books[1], authors[1], R.drawable.girl_train));
bookList.add(new Book(books[2], authors[2], R.drawable.harry_potter));
bookList.add(new Book(books[3], authors[3], R.drawable.hunder_games));
bookList.add(new Book(books[4], authors[4], R.drawable.lord_rings));
bookList.add(new Book(books[5], authors[5], R.drawable.moby_dick));
bookList.add(new Book(books[6], authors[6], R.drawable.mocking_bird));
bookList.add(new Book(books[7], authors[7], R.drawable.the_godfather));
@Override
public void onClick( Book book)
// here I want to use switch statment for clicking individual components and taking result respectively
showToast(book.getBookTitle());
showToast(book.getBookAuthor());
【问题讨论】:
这能回答你的问题吗? Multiple onClickListener in a RecyclerView 【参考方案1】:你有错误的逻辑。
您需要像这样更改点击监听器实现:
将这两行放在开关盒之前。
title_textview.setOnClickListener(this);
author_textview.setOnClickListener(this);
在您的 ViewHolder 类中实现 View.OnClickListener
,然后覆盖 onClick()
并像这样更改您的开关盒。
@Override
public void onClick(View view)
switch (view.getId())
case R.id.title_textview:
String bookTitle = bookList.get(getAdapterPosition()).getBookTitle();
showToast(bookTitle);
break;
case R.id.author_textview:
String bookAuthor = bookList.get(getAdapterPosition()).getBookAuthor();
showToast(bookAuthor);
break;
是的,如果您不处理布局更改,则应该使用getAdapterPosition()
,因为大多数时候最好只使用与数据相关的东西。
【讨论】:
谢谢它真的很有帮助 很高兴它有帮助:)以上是关于如何在recyclerview android中的single_row_item的每个视图(imageView,textView)上添加不同的点击事件?的主要内容,如果未能解决你的问题,请参考以下文章
当sqlite android片段中的数据更改或删除时如何刷新recyclerview?
如何在 RecyclerView android kotlin 中的每个项目上显示日期(日和月)
Android TV 中的 RecyclerView 项目焦点
一起Talk Android吧(第五百零一回:如何正确地更新RecyclerView中的数据)
如何将 Web 服务数据传递到 Android 中的 RecyclerView?
如何解决 recyclerView Android 中的 addInArray 异常和 removeFromArray 异常?