Android: 取消-select 除了当前的所有 CheckedTextView

Android: Un-select all CheckedTextView except present one

我正在 select 使用下面的代码(这基于 CheckedTextView)来处理 ListView 项目

 lvReport.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

        rlAdapterReport = (RelativeLayout) view.findViewById(R.id.rl_lv_report_user);
        tvAdapterReportOption = (CheckedTextView) view.findViewById(R.id.tv_lv_report_user_title);

        if (tvAdapterReportOption.isChecked()) {
            //value = "un-Checked";
            tvAdapterReportOption.setCheckMarkDrawable(0);
            tvAdapterReportOption.setTextColor(Color.parseColor("#000000"));
            rlAdapterReport.setBackgroundColor(Color.parseColor("#FFFFFF"));
            tvAdapterReportOption.setChecked(false);
        } else {
            //value = "Checked";

            Drawable dr = getResources().getDrawable(R.drawable.report_tick_icon);
            Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
            Drawable finalDrawable = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, 50, 50, true));

            tvAdapterReportOption.setCheckMarkDrawable(finalDrawable);
            tvAdapterReportOption.setTextColor(Color.parseColor("#04CFE7"));
            rlAdapterReport.setBackgroundColor(Color.parseColor("#D5D5D5"));
            tvAdapterReportOption.setChecked(true);
        }
    }
});

通过使用上面的代码,我可以 select 所有项目(为了理解,请参阅附图),但我想一次 select 单个项目。所以,如果已经 selected

,我想取消 select 所有项目

编辑

我的适配器(ArrayAdapter)getView代码

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;

final ReportUserItems rowItem = getItem(position);

LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null){
    convertView = mInflater.inflate(R.layout.lv_report_user_adapter, null);
    holder = new ViewHolder();

    holder.rlReport = (RelativeLayout) convertView.findViewById(R.id.rl_lv_report_user);
    holder.tvReportOption = (CheckedTextView) convertView.findViewById(R.id.tv_lv_report_user_title);

    convertView.setTag(holder);
}
else{
    holder = (ViewHolder) convertView.getTag();
}

return convertView;
}

在您的适配器中 class 存储选定的行位置,并在用户单击其他项目时更新和刷新。

您的 Adpater class 发生了变化。

int selectedPosition = -1;  //to store only selected item position

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    final ReportUserItems rowItem = getItem(position);
    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        //your code
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    //your other code 


    if (selectedPosition != -1)
        if (selectedPosition == position) {
            //your drawable code
            holder.tvReportOption.setCheckMarkDrawable(finalDrawable);
            holder.tvReportOption.setChecked(true);
            //your other stuff : changing color etc
        } else {
            holder.tvReportOption.setCheckMarkDrawable(0);
            holder.tvReportOption.setChecked(false);
            //your other stuff : changing color etc
        }

    return convertView;
}

//create this method in Adapter class
public void setSelected(int pos) {
    selectedPosition = pos;  //change selected item position
    notifyDataSetChanged();  //refresh views
}

Activity 代码更改

lvReport.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        adapter.setSelected(position);
    }
});