我已经使用cardview创建了一个recyclerview,并在recyclerview的每个项目中添加了按钮。我已经集成了按钮点击事件。现在,当我点击其中一个recyclerview项目的按钮时,它会正常响应并且其他项目的按钮同时响应,就好像它们也被点击一样。问题是什么?怎么解决?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="95dp"
android:orientation="horizontal"
android:weightSum="3">
<android.support.v7.widget.CardView
android:id="@+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
app:cardCornerRadius="1dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:clickable="true">
<ImageView
android:id="@+id/icon_vendor1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:layout_marginRight="10dp"
android:padding="0dp"
android:scaleType="fitXY"
android:src="@drawable/ic_direction" />
<TextView
android:id="@+id/vendorName1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_toRightOf="@+id/icon_vendor1"
android:text="Kozzaja Infotech Pvt Ltd"
android:textSize="20sp" />
<RatingBar
android:id="@+id/rating"
style="@style/RatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/vendorName1"
android:layout_marginTop="4dp"
android:layout_toEndOf="@+id/imageView2"
android:layout_toRightOf="@+id/icon_vendor1" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="38dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/vendorName1"
android:layout_marginBottom="3dp"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/layout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageButton
android:id="@+id/button4"
android:layout_width="38dp"
android:layout_height="wrap_content"
android:background="@drawable/circle_button"
android:padding="10dp"
android:src="@drawable/ic_call_black_36dp" />
<ImageButton
android:id="@+id/button3"
android:layout_width="38dp"
android:layout_height="wrap_content"
android:background="@drawable/circle_button"
android:padding="20dp"
android:src="@drawable/ic_messenger_outline_black_36dp" />
<ImageButton
android:id="@+id/button2"
android:layout_width="38dp"
android:layout_height="match_parent"
android:background="@drawable/circle_button"
android:src="@drawable/ic_directions_black_24dp" />
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="38dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/layout"
android:background="@drawable/circle_button"
android:tag="off"
android:text="+"
android:textSize="30sp" />
<View
android:layout_width="11dp"
android:layout_height="38dp"
android:background="#ffffff" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
这是我的适配器代码。
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.title.setText(workers.get(position).getName());
holder.rating.setRating(workers.get(position).getRatings());
Picasso.with(context).load(workers.get(position).getImageLogo()).fit().into(holder.imageView);
holder.plus.setTag("off");
holder.plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/// button click event
if (holder.plus.getTag().toString().equals("off")) {
holder.plus.setTag("on");
holder.plus.setText("-");
//show the options button using animation
holder.layout.startAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.left_to_right));
holder.layout.setVisibility(View.VISIBLE);
} else if (holder.plus.getTag().toString().equals("on")) {
holder.plus.setTag("off");
holder.plus.setText("+");
//hide the options button using animation
holder.layout.startAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.right_to_left));
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//disappear the button after 500ms
holder.layout.setVisibility(View.GONE);
}
}, 500);
}
}
});
}