我正在尝试设置一个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并将持有者设置更改为缩略图会更容易吗?
答案 0 :(得分:0)
您不应使用“ 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
}
}
}
您应该直接使用Listener / Callback机制将单击处理委托给Adapter的父活动/片段,而不是直接处理Adapter中的单击。