带有复选框问题的Android回收站视图

时间:2017-03-06 12:29:35

标签: android android-recyclerview

我的RecyclerView中有一个MainActivity adapter classRecyclerView的每个列表单元格都包含Checkbox。为Checkbox设置了一个预选值。如果为真,则设置Checkbox。还有一件事是一些项目专门用于单一选择(即,如果我选择这些项目' Checkbox,其他复选框将被取消选中)。该值由每个项目字典中的另一个参数确定。而且我的活动中有一个按钮,当没有选择任何按钮时,按钮将被禁用,反之亦然。我该如何实现这些功能?我的课程如下。

public class FurtherExtrasAdapter extends RecyclerView.Adapter<FurtherExtrasAdapter.ViewHolder> {


ArrayList<Item> items;
Context context;
int flag = 0;
private CouponCodeActivity mThis;

public FurtherExtrasAdapter(ArrayList<Item> items, CouponCodeActivity mThis, Context context) {
    this.items = items;
    this.mThis = mThis;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_cell_platform_item, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {

    ;
    holder.enrolment.setText(items.get(position).getName());
    holder.selectEnrollment.setChecked(items.get(position).isPreselect());

}

@Override
public int getItemCount() {
    return items.size();
}


@Override
public long getItemId(int position) {
    return super.getItemId(position);
}

public class ViewHolder extends RecyclerView.ViewHolder {
    TextView enrolment;
    CheckBox selectEnrollment;

    public ViewHolder(View itemView) {
        super(itemView);
        enrolment = (TextView) itemView.findViewById(R.id.tv_entrollment);
        selectEnrollment = (CheckBox) itemView.findViewById(R.id.cb_select_entrollment);


    }
}}

我的视图类包含以下代码。

  rv_further=(RecyclerView)findViewById(R.id.rv_further);
  rv_further.setLayoutManager(manager);
            rv_further.setVisibility(View.VISIBLE);
            rv_further.setHasFixedSize(true);
            rv_further.setItemAnimator(new DefaultItemAnimator());
            rv_further.setAdapter(adapter);

 public void updateButtonState(double num) {

    checkedBoxs += num;

        if (checkedBoxs > 0) {
            Log.d("---CHECKED",String.valueOf(checkedBoxs));
            btnApply.setEnabled(true);
        } else {
            Log.d("---UNCHECKED",String.valueOf(checkedBoxs));
            btnApply.setEnabled(false);
        }
}

2 个答案:

答案 0 :(得分:0)

使用check clicklistener,如...

holder.selectEnrollment .setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            items.get(position).setPreselect(!items.get(position).isPreselect);
        }
    });

答案 1 :(得分:0)

基于此:

  

然后我们可以只检查/选择那个检查按钮。其他人应该自动取消选择。

您可以在适配器中保留一个变量,比如说

int position_last_selected = -1;

然后在添加时,检查此值是否为-1。如果没有,请获取该职位的项目,

list.get(position_last_selected).setIsPreSelect(false);
// then update your current position item
list.get(position).setIsPreSelect(true);
// update the last selected value
position_last_selected = position;
//Update the adapter
notifiyDataSetChanged();

尝试一下!