【发布时间】: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
标签: android android-recyclerview interface switch-statement onitemclicklistener