Android: Un-select all CheckedTextView except present one

眉间皱痕 提交于 2019-12-12 02:34:25

问题


i'm selecting the ListView items by using the bellow code (This is based on 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);
        }
    }
});

By using above code i'm able to select all items (For understanding see the attached image), but i want to select the single item at a time. So, i want to deselect the all items if already selected

Edit

My adapter (ArrayAdapter) getView code

@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:


In your adapter class store the selected row position and update and refresh whenever user clicks on other item.

Your Adpater class changes.

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 code changes

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


来源:https://stackoverflow.com/questions/37300153/android-un-select-all-checkedtextview-except-present-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!