简体   繁体   中英

RecyclerView choose every 8th item in list. How can stop it?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM