RecyclerView选择列表中的第8个项目。如何阻止它?

时间:2019-06-14 06:42:07

标签: java android android-recyclerview android-cardview

我的项目中有一个recyclerView。另外,我从第三个库中添加了expandableLayout。单击其中的layout的{​​{1}}时,它是cardView toggle。但是,例如,如果我单击第一个expandable layout,则在第一个item之后按顺序每{8个toggle item单击一次。如何解决并停止其他itemtoggle

items

1 个答案:

答案 0 :(得分:1)

尝试为您的数据集的Bus类添加一个布尔字段“ isExpanded”。在适配器的onBindViewHolder中,根据数据的isExpanded标志设置UI。然后在单击总线项目时,根据需要将其isExpanded设置为true或false或!isExpanded,然后使适配器notifyDataSetChanged()。
试试:
修改您的对象类,添加以下标志。

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

在适配器中:

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