如何在多个选项上显示复选框

时间:2016-06-07 05:42:32

标签: android

“我有一个通用控制器。其中有2个字段。文本字段和复选框。”我的问题是“当用户选择单个项目时,复选框不应该是可见的,当用户选择多个项目时,复选框应该是可见的。”这是在android中。请指导我完成这个。

代码如下:

公共类ProfileItemDialogFilterableAdapter扩展了RecyclerView.Adapter {

List<SelectableItem> mItemList;
private LayoutInflater mLayoutInflater;
public static int mSingleSelection = 1;
public static int mMultipleSelection = 2;
private int mSelectedPosition;
private int mSelectionMode;
private Context mContext;

public ProfileItemDialogFilterableAdapter(Context context, List<SelectableItem> itemList) {
    mItemList = itemList;
    mLayoutInflater = LayoutInflater.from(context);
    mContext = context;

}

@Override
public ProfileItemDialogFilterableAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemLayoutView = mLayoutInflater.inflate(R.layout.list_item_profile_item_dialog, parent, false);

    return new ViewHolder(itemLayoutView);
}

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

    SelectableItem current = mItemList.get(position);

    if (current == null)
        return;

    holder.mLabelName.setText(current.getName());

    if (mSelectionMode == mMultipleSelection){

        holder.mCheckBoxItem.setVisibility(View.VISIBLE);
        holder.mCheckBoxItem.setChecked(current.isSelected());
    }
    else {
        holder.mCheckBoxItem.setVisibility(View.GONE);

        if (position == mSelectedPosition) {
            holder.mLabelName.setTextColor(mContext.getResources().getColor(R.color.colorBackground));
            holder.mLabelName.setBackgroundColor(mContext.getResources().getColor(R.color.colorPrimary));

        } else {
            holder.mLabelName.setTextColor(mContext.getResources().getColor(R.color.textColorFullBlack));
            holder.mLabelName.setBackgroundColor(mContext.getResources().getColor(R.color.colorBackground));
        }
    }
}

@Override
public int getItemCount() {
    return (mItemList != null ? mItemList.size() : 0);
}

public void setResult(List<SelectableItem> itemList) {
    mItemList = itemList;
    notifyDataSetChanged();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    CheckBox mCheckBoxItem;
    TextView mLabelName;

    public ViewHolder(View itemView) {
        super(itemView);

        mCheckBoxItem = (CheckBox) itemView.findViewById(R.id.checkbox_item);
        mLabelName = (TextView) itemView.findViewById(R.id.label_name);
        mCheckBoxItem.setOnCheckedChangeListener(onCheckedChangeListener);
        mLabelName.setOnClickListener(onClickListener);
    }

    CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            mItemList.get(getAdapterPosition()).setSelected(isChecked);

        }
    };

    View.OnClickListener onClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            mSelectedPosition = getAdapterPosition();
            notifyDataSetChanged();
        }
    };
}

}

1 个答案:

答案 0 :(得分:0)

在您的选择中添加listener(单个或多个),在listener内更改checkbox的可见性。