如何将 onClick 侦听器应用于 CardView 中的图像视图



我正在尝试设置一个onClick监听器,该监听器已经有一个按钮。目的是提供第二种访问意图的方法(在本例中为"ReadActivity"类。这方面的一个例子是

这是我的代码:

@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.tv_book_title.setText(mData.get(position).getBookTitle());
holder.tv_book_author.setText("by " + mData.get(position).getBookAuthor());
holder.img_book_thumbnail.setImageResource(mData.get(position).getBookId());
holder.actionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, ReadActivity.class);
// passing data to the book activity
intent.putExtra("Id", mData.get(position).getPhotoId());
intent.putExtra("Title", mData.get(position).getBookTitle());
intent.putExtra("Author", mData.get(position).getBookAuthor());
intent.putExtra("Description", mData.get(position).getContentUrl());
intent.putExtra("Thumbnail", mData.get(position).getBookId());
// start the activity
mContext.startActivity(intent);
}
});

这是我cardview_item xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="@dimen/card_height"
android:layout_gravity="center"
android:layout_marginBottom="@dimen/md_keylines"
android:layout_marginLeft="@dimen/md_keylines"
android:layout_marginRight="@dimen/md_keylines"
android:foreground="?attr/selectableItemBackground">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/book_img_id"
**android:onClick="imageClick"**
android:layout_width="match_parent"
android:layout_height="@dimen/card_image_height"
android:scaleType="centerCrop"
android:contentDescription="@string/todo" />
<TextView
android:id="@+id/book_title_id"
android:layout_width="match_parent"
android:layout_height="@dimen/card_title_height"
android:layout_alignBottom="@+id/book_img_id"
android:layout_marginStart="@dimen/md_keylines"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:textColor="@color/white" />
<TextView
android:id="@+id/book_author_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/book_img_id"
android:layout_marginLeft="@dimen/md_keylines"
android:layout_marginTop="@dimen/md_keylines"
android:layout_marginBottom="@dimen/md_keylines"
android:layout_marginRight="@dimen/md_keylines"
android:ellipsize="end"
android:singleLine="true"
android:textColor="@color/dark_grey"
android:textSize="@dimen/article_subheading" />
<Button
android:id="@+id/action_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/book_author_id"
style="?android:attr/borderlessButtonStyle"
android:textColor="?attr/colorPrimary"
android:text="@string/read" />
</RelativeLayout>
</android.support.v7.widget.CardView>

在 xml 中,我尝试设置 onClick 行,但我不确定如何设置 onClick 侦听器。有没有办法在不影响原始代码的情况下做到这一点?或者复制到actionButton.setOnClickListener并将持有人设置更改为缩略图会更容易吗?

您不应该使用 'android:onClick' 属性来处理点击。您最好在代码中处理它,以便布局和逻辑不会紧密耦合。

您需要像在actionButton一样使用 setOnClickListener。或者,您可以稍微更改代码以处理 ViewHolder 中的单击。像这样更改代码:

// use View.OnClickListener so that the click listener won't
// be recreated when the View is recycled
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public Button actionButton;
public ImageView img_book_thumbnail;
public ViewHolder(Context context, View itemView) {
super(itemView);
// find actionButton and img_book_thumbnail
...
// Attach a click listener to both of them
actionButton.setOnClickListener(this);
img_book_thumbnail.setOnClickListener(this);
}
// Handles clicked
@Override
public void onClick(View view) {
if(view.getId == R.id.action_button) {
// do something
} else if(view.getId == R.id.book_img_id) {
// do something
}
}
}

您应该使用侦听器/回调机制将单击处理委托给适配器的父活动/片段,而不是直接处理适配器中的单击。

最新更新