问题描述
我在项目中实现了recyclerView
.
我的recyclerView
row
中有一个Button
.我的recyclerView
每行代码如下:
I have a Button
in my recyclerView
row
. The code for my each row of recyclerView
is like this:
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缓存的原因.
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中更改视图的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!