Android:取消选择除现有的CheckedTextView之外的所有CheckedTextView

时间:2016-05-18 12:49:32

标签: android listview onitemclicklistener checkedtextview

我正在使用下面的代码选择ListView项目(这是基于CheckedTextView)

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

通过使用上面的代码,我可以选择所有项目(为了便于查看附图),但我想一次选择单个项目。所以,我想取消选择已经选中的所有项目

enter image description here

修改

我的适配器(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;
}

1 个答案:

答案 0 :(得分:2)

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

您的Adpater课程发生了变化。

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
}

活动代码更改

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