1

This was an answer posted in a question which is about repeating button views in certain positions . Code works fine. My doubt is how to hold a visibility of first item even after scrolling in recyclerview?

 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);
        }
    }

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);
        }
    });
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
SARATH V
  • 500
  • 1
  • 7
  • 33
  • you can use sticky header for doing this. check this out: (https://github.com/bgogetap/StickyHeaders) – M.Usman Sep 18 '17 at 12:49
  • I got what you need! I guess you need to understand the explanation given [here](https://stackoverflow.com/questions/32949971/how-can-i-make-sticky-headers-in-recyclerview-without-external-lib) – Sumit Shetty Sep 18 '17 at 13:20

3 Answers3

2

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>
Kuffs
  • 35,581
  • 10
  • 79
  • 92
0

Set an array of boolean variables associated with each item.

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {

    if(visibilityList.get(position)){
        holder.button.setVisibility(View.VISIBLE); 
    }else{
        holder.button.setVisibility(View.GONE); 
    }

    holder.message.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(visibilityList.get(position)){
                visibilityList.set(position, false);
                holder.button.setVisibility(View.GONE);
            }else{
                visibilityList.set(position, true);
                holder.button.setVisibility(View.VISIBLE);
            }

        }
    });
}

Note: visibilityList is the List variable where each value is set to default (either true or false as per your requirement)

Kunu
  • 5,078
  • 6
  • 33
  • 61
0

Use HashMap to keep those positions which you need to show. Write code in onBindViewHolder method

if(map.contains(holder.getAdapterPosition()){
    holder.btn.setVisibility(View.VISIBLE);
} else {
    holder.btn.setVisibility(View.GONE);
}

Note: - do write else case too, otherwise recyclerView will misbehave due to reusability.

Harish Gyanani
  • 1,366
  • 2
  • 22
  • 43