I assume that in your Adapter, you hold an array of objects that represents the items you want to be displayed.
Add a property to this object named for example ButtonVisible
and set the property when you press the button.
Complete sample adapter follows. This displays a list of items with a button that, when pressed, is made non-visible. The visibility is remembered no matter how many items in the list or how much you scroll.
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.VH> {
public static class MyData {
public boolean ButtonVisible = true;
public String Text;
public MyData(String text) {
Text = text;
}
}
public List<MyData> items = new ArrayList<>();
public TestAdapter() {
this.items.add(new MyData("Item 1"));
this.items.add(new MyData("Item 2"));
this.items.add(new MyData("Item 3"));
}
@Override
public TestAdapter.VH onCreateViewHolder(ViewGroup parent, int viewType) {
return new VH((
LayoutInflater.from(parent.getContext())
.inflate(R.layout.test_layout, parent, false))
);
}
@Override
public void onBindViewHolder(TestAdapter.VH holder, final int position) {
final MyData itm = items.get(position);
holder.button.setVisibility(itm.ButtonVisible ? View.VISIBLE : View.GONE);
holder.text.setText(itm.Text);
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
itm.ButtonVisible = false;
notifyItemChanged(position);
}
});
}
@Override
public int getItemCount() {
return items.size();
}
public class VH extends RecyclerView.ViewHolder {
Button button;
TextView text;
public VH(View itemView) {
super(itemView);
button = itemView.findViewById(R.id.toggle);
text = itemView.findViewById(R.id.text1);
}
}
}
test_layout.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="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>