0

I have an issue with recycler view. I implemented a collapse logic as you can see on the code below. But when I close the second item the view disappear as you can see on the video. What am I doing wrong. Please assist. Thanks

 public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

    View v = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.report_layout, viewGroup, false);

    final ReportHolder holder = new ReportHolder(v);

    //hide half of the view
    holder.linearLayout.setVisibility(View.GONE);

    holder.tvPrintReceipt.setVisibility(View.INVISIBLE);

    holder.tvClose.setOnClickListener(v1 -> {
        TransitionManager.beginDelayedTransition(viewGroup, new AutoTransition());
        holder.linearLayout.setVisibility(View.GONE);
        holder.tvViewRecords.setVisibility(View.VISIBLE);
    });

    holder.tvViewRecords.setOnClickListener(v2 ->{
        TransitionManager.beginDelayedTransition(viewGroup, new AutoTransition());
        holder.linearLayout.setVisibility(View.VISIBLE);
        holder.tvViewRecords.setVisibility(View.GONE);
    });

    return holder;
}

2 Answers2

0

In order to not have problems with this you should set the visibility of your entire item to GONE. itemView.setVisibility(View.GONE); or view.setVisibility(View.GONE);

Also check this thread for more information How to hide an item from Recycler View on a particular condition?

MihaiBC
  • 472
  • 5
  • 15
0

The reason of this problem is that you wrote your logic in onCreatViewHolder which will be called once and you have to move it to onBindViewHolder which will be called per each item in RecyclerView. This will solve your issue. Now, clicking on each item, makes change on all items in the list.

meysam ghorbani
  • 199
  • 1
  • 4
  • I moved the logic to `onBindViewHolder` , but still didn't work, then I added notifyItemChange(), so now when I collapse the second item it did not disappear. but the issue now is that the first item now does not expand, when you click on the view Record expand text it expands the second item instead. – codewithbawo Dec 31 '21 at 14:06
  • Please share the whole logic of your adapter, then I might be able to help. Because notifying changes of items only is important when it's related to data. – meysam ghorbani Jan 12 '22 at 14:15