Android:更改 recyclerView 中视图的可见性 [英] Android: Changing visibility of a view in recyclerView
问题描述
我在我的项目中实现了一个 recyclerView
.
I have implemented a recyclerView
in my project.
我的 recyclerView
row
中有一个 Button
.我的recyclerView
每一行的代码是这样的:
I have a Button
in my recyclerView
row
. The code for my each row of recyclerView
is like this:
保存的message_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>
按钮的可见性消失
.当有人单击其上方的 message
textView
时,我想将此按钮的可见性更改为可见".我在 message (textView)
上实现了一个简单的 onClickLiestener()
并在点击 message
时更改了 button
的可见性代码>.我知道这行不通,但我想看看结果.结果很奇怪.如果我点击第 1 行的 textView,第 7、17、19 行等的按钮也 变得可见.我猜这可能是因为缓存了 viewHolder.
The visibility of button is gone
. I want to change the visibility of this button to 'visible' when someone clicks on the message
textView
above it. I implemented a simple onClickLiestener()
on the message (textView)
and changed the visibility of button
on click of the message
. I knew that wasn't going to work but I wanted to see the results. The results are weird. If I click on the textView of row 1, the button of row 7,17,19 etc also becomes visible. I can guess this might be coz of caching of the viewHolder.
MyViewHolder 是这样的:
MyViewHolder is something like this:
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的仅特定行按钮的可见性?
Can someone guide me how can I change the visibility of a button, of a perticular row only, of my recyclerView?
推荐答案
将点击逻辑从 ViewHolder 移开:
Move the click logic away from the ViewHolder:
class MyViewHolder extends RecyclerView.ViewHolder {
TextView message;
Button button;
public MyViewHolder(View itemView) {
super(itemView);
message = (TextView) itemView.findViewById(R.id.message);
button = (Button) itemView.findViewById(R.id.button);
}
}
并将其放在适配器的 onBindViewHolder 方法中:
and place it inside the method onBindViewHolder of your adapter:
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
holder.button.setVisibility(View.GONE);
holder.message.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.button.setVisibility(View.VISIBLE);
}
});
}
ViewHolder 被 RecyclerView 重用,这就是您看到按钮在其他行中可见的原因.
ViewHolder is reused by the RecyclerView, that's why you are seeing the button visible in other rows.
这篇关于Android:更改 recyclerView 中视图的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!