0

There is an recyclerView in my project. Also, I added expandableLayout from a third library. When click layout of it has cardView, it toggle expandable layout. But, for example if click 1st item, it toggle every 8th item in order after 1st item. How can solve it and stop toggle other items?

public class BusAdapter extends RecyclerView.Adapter<BusAdapter.ViewHolder> {
        private Context context;
        private List<Bus> busList;
        private static String phoneNumber;
        private static final int PERMISSION_CODE = 101;
        private RecyclerView recyclerView;
        private Locale trlocale;

        VoyageAdapterToResultActivityListener voyageAdapterToResultActivityListener;

        public BusAdapter(Context _context, List<Bus> _busList, RecyclerView recyclerView) {
            this.context = _context;
            this.busList = _busList;
            this.recyclerView = recyclerView;
            this.trlocale = new Locale("tr-TR");
            voyageAdapterToResultActivityListener = (VoyageAdapterToResultActivityListener) context;
        }

        public class ViewHolder extends RecyclerView.ViewHolder {

            private TextView tvPrice, text_seat;
            private ImageView imgLogo;

            private ConstraintLayout relativeL_row_voyage_view;
            private CardView cardV_row_voyage;
            ExpandableLayout expandable_layout;
            ProgressBar progressB_dialog_seat;
            private ViewHolder(View view) {
                super(view);view.findViewById(R.id.relative_seat);
                expandable_layout = view.findViewById(R.id.expandable_layout);
                progressB_dialog_seat = view.findViewById(R.id.progressB_dialog_seat);
                relativeL_row_voyage_view = view.findViewById(R.id.relativeL_row_voyage_view);

                relativeL_row_voyage_view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        expandable_layout.toggle();
                        progressB_dialog_seat.setVisibility(View.VISIBLE);

                        final Handler h = new Handler() {
                            @Override
                            public void handleMessage(Message message) {
                                progressB_dialog_seat.setVisibility(View.INVISIBLE);

                            }
                        };
                        h.sendMessageDelayed(new Message(), 5000);
                    }
                });
Ahmet Yılmaz
  • 425
  • 5
  • 16
  • ViewHolder is beeing recycled, that means, that when you scroll, cells going away on top are reused on bottom. You need to reset each cell into proper state in `onBindViewHolder`. You can keep list of expanded buses - in onClick add or remove bus from expanded collection and in method `onBindViewHolder` set expandable_layout expanded/collapsed. – egoldx Jun 14 '19 at 06:58
  • 1
    I blogged about a similar case, this article can help you https://android.jlelse.eu/android-handling-checkbox-state-in-recycler-views-71b03f237022 – oziomajnr Jun 14 '19 at 07:00
  • thank you both of you. @Ogbe I solved with first solution in your blog. But, you don't recommend it so I will try other one. – Ahmet Yılmaz Jun 14 '19 at 07:26

1 Answers1

1

Try add a boolean field "isExpanded" to class Bus for your data set. In onBindViewHolder of adapter, setup UI depending on data's isExpanded flag. And when click bus item, set its isExpanded to true or false or !isExpanded as you wish, then make adapter notifyDataSetChanged().
Try:
Modify your object class, add following flags.

public class Bus {
    ...
    boolean isExpanded;
    boolean isShowProgress;

    public boolean isExpanded() {
        return this.isExpanded;
    }

    public void setExpanded(boolean expanded) {
        this.isExpanded = expanded;
    }

    public boolean isShowProgress() {
        return this.isShowProgress;
    }

    public void setShowProgress(boolean showProgress) {
        this.isShowProgress = showProgress;
    }
}

In your adapter:

public class BusAdapter extends RecyclerView.Adapter<BusAdapter.ViewHolder> {
    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tvPrice, text_seat;
        private ImageView imgLogo;
        private ConstraintLayout relativeL_row_voyage_view;
        private CardView cardV_row_voyage;
        ExpandableLayout expandable_layout;
        ProgressBar progressB_dialog_seat;

        private ViewHolder(View view) {
            super(view);
            view.findViewById(R.id.relative_seat);
            expandable_layout = view.findViewById(R.id.expandable_layout);
            progressB_dialog_seat = view.findViewById(R.id.progressB_dialog_seat);
            relativeL_row_voyage_view = view.findViewById(R.id.relativeL_row_voyage_view);
        }
    }

    public void onBindViewHolder(ViewHolder holder, int i) {
        Bus bus = busList.get(i);

        if (bus.isExpanded()) {
            holder.expandable_layout.expand();
        } else {
            holder.expandable_layout.collapse();
        }
        holder.progressB_dialog_seat.setVisibility(bus.isShowProgress() ? View.VISIBLE : View.INVISIBLE);

        holder.relativeL_row_voyage_view.setOnClickListener(new View.OnClickListener() {
            bus.setExpanded(!bus.isExpanded());
            bus.setShowProgress(true);
            notifyDataSetChanged();

            final Handler h = new Handler() {
                @Override
                public void handleMessage(Message message) {
                    bus.setShowProgress(false);
                    notifyDataSetChanged();
                }
            };
            h.sendMessageDelayed(new Message(), 5000);
        });
        ...
    }  
}     
Ahmet Yılmaz
  • 425
  • 5
  • 16
TylerQITX
  • 318
  • 2
  • 9