Android : RecyclerView row Visibility not working properly

Viewed 2767

I am developing an android app (Android Studio). I want to implement Visibility GONE/VISIBLE in every row. In the image there are three recyclerView rows,

I want to do the following things :

There are two part in every row Heading & Details.

  1. When This Activity open all the rows details should hide.
  2. When I click on particular row , details of the row should appear by Visibility VISIBLE.
  3. When i again click on row details should disappear by Visibility GONE.

    Problem is : If i clicked on first row, Details of first row is showing and without hiding details of first row when click on next any row Heading. I have to click two times to show the details of the next row or Hide the details of next row.

    Image One Here

    Image Two Here

Adapter Code :

int i = 0;
holder.salt_head_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                i++;
                if (i % 2 == 1) {
                    holder.have_show_ll.setVisibility(View.VISIBLE);
                    holder.l1_arrow.setImageResource(R.drawable.down_arrow_black);

                } else if (i % 2 == 0) {
                    holder.have_show_ll.setVisibility(View.GONE);
                    holder.l1_arrow.setImageResource(R.drawable.right_arrow_black);

                }
            }
        });
4 Answers

Try adding a flag in the code and based on that add logic of visiblity in onBindViewHolder.

@Override
public void onBindViewHolder(@NonNull final ProductViewHolder holder, final int position) {
holder.linearLayoutItemButton.setVisibility(canShow(productOrderList.get(position))?
            View.VISIBLE : View.GONE);
holder.addButton.setVisibility(canShow(productOrderList.get(position))?
            View.GONE : View.VISIBLE);
Related