【发布时间】:2016-09-18 16:27:35
【问题描述】:
我在我的项目中实现了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 时,我想将此按钮的可见性更改为“可见”。我在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 的仅特定行按钮的可见性?
【问题讨论】:
-
请发布您的适配器代码。您需要添加一个属性来模拟按钮的可见状态。
标签: android android-recyclerview android-view android-viewholder