我在我的项目中实现了一个recyclerView
。
我在recyclerView
的row
中有一个Button
。每一行recyclerView
的代码如下:
savedmessage_custom_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:background="#ffffff"
android:orientation="vertical">
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="Dummy text" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:visibility="gone"/>
</LinearLayout>
按钮的可见性是“gone”。我想在某人点击其上方的“message textView”时,将此按钮的可见性更改为“visible”。我在“message(textView)”上实现了一个简单的“onClickLiestener()”,并在点击“message”时更改了“button”的可见性。我知道那不会起作用,但我想看看结果。结果很奇怪。如果我点击第1行的textView,则第7、17、19等行的按钮也会变为可见状态。我猜这可能是viewHolder缓存的原因。
MyViewHolder大致如下:
class MyViewHolder extends RecyclerView.ViewHolder {
TextView message;
public MyViewHolder(final View itemView) {
super(itemView);
message = (TextView) itemView.findViewById(R.id.message);
message.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
itemView.findViewById(R.id.button).setVisibility(View.VISIBLE);
}
});
}
}
有人能指导我如何更改recyclerView中特定行的按钮的可见性吗?