Android - 单选和多选CheckedTextView

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28261207/article/details/83095851

布局1 : ListView

<ListView
    android:id="@+id/lv_edit_select"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/dp_3"
    android:background="@color/white"
    android:divider="@drawable/listview_divider"
    android:dividerHeight="@dimen/dp_1"
    android:listSelector="@color/trans"/>

布局2 : item布局

<?xml version="1.0" encoding="utf-8"?>
<utils.ImageCheckedTextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tv_single_choice"
    android:layout_width="match_parent"
    android:layout_height="@dimen/dp_50"
    android:drawablePadding="@dimen/dp_10"
    android:gravity="center_vertical"
    android:paddingLeft="@dimen/dp_28"
    android:paddingRight="@dimen/dp_22"
    android:textSize="@dimen/sp_16">
</utils.ImageCheckedTextView>

item布局里的自定义类 :

package utils;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.widget.CheckedTextView;

import xxx.xxxx.xxxxxx.R;

/**
 * Created by LXL on 2018/5/17.
 */
public class ImageCheckedTextView extends CheckedTextView {

    private Drawable drawableLeft;
    private int scaleWidth; //dp值
    private int scaleHeight;

    public ImageCheckedTextView(Context context) {
        super(context);
    }

    public ImageCheckedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ImageCheckedTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public ImageCheckedTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    public void init(Context context, AttributeSet attrs) {
        //        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ImageTextButton);


        drawableLeft = context.getResources().getDrawable(R.drawable.select_detail_edit);
        //
        //        drawableLeft = typedArray.getDrawable(R.styleable.ImageTextButton_leftDrawable);
        //        scaleWidth = typedArray.getDimensionPixelOffset(R.styleable
        //                .ImageTextButton_drawableWidth, UIUtils.dip2px(20));
        //        scaleHeight = typedArray.getDimensionPixelOffset(R.styleable
        //                .ImageTextButton_drawableHeight, UIUtils.dip2px(20));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (drawableLeft != null) {
            //            drawableLeft.setBounds(0, 0, UIUtils.dip2px(scaleWidth), UIUtils.dip2px(scaleHeight));
            drawableLeft.setBounds(0, 0, 48, 48);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        this.setCompoundDrawables(drawableLeft, null, null, null);
    }

    /**
     * 设置左侧图片并重绘
     *
     * @param drawableLeft
     */
    public void setDrawableLeft(Drawable drawableLeft) {
        this.drawableLeft = drawableLeft;
        invalidate();
    }

    /**
     * 设置左侧图片并重绘
     *
     * @param drawableLeftRes
     */
    public void setDrawableLeft(int drawableLeftRes) {
        this.drawableLeft = getContext().getResources().getDrawable(drawableLeftRes);
        invalidate();
    }

}

自定义类里的 R.drawable.select_detail_edit.xml :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/white" android:state_checked="false"></item>
    <item android:drawable="@mipmap/ic_select_v" android:state_checked="true"></item>

</selector>

item布局里的选中图片 ic_select_v.png :
ic_select_v.png

使用 :

  • 如果是单选 :
lvSingleChoice.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
lvSingleChoice.setAdapter(new ArrayAdapter<>(mContext, R.layout.item_lv_single_choice, mLabelList));
//            lvSingleChoice.setItemChecked(mValueList.indexOf(mValue), true);
//            LogUtil.e(TAG, "mValueList.indexOf(mValue) --- " + mValueList.indexOf(mValue));

lvSingleChoice.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        int checkedItemPosition = lvSingleChoice.getCheckedItemPosition();
        mValue = mValueList.get(checkedItemPosition);
    }
});
  • 如果是多选 :
lvSingleChoice.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
lvSingleChoice.setAdapter(new ArrayAdapter<>(mContext, R.layout.item_lv_single_choice, mLabelList));
lvSingleChoice.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //通过listView对象获取到当前listView中被选择的条目position;
        //以下方法实现会对返回一个SparseBooleanArray集合,其中对listview的触发过点击事件的每个条目进行
        // 标记(键值对)键==position/值==boolean,若该条目被选中则显示true,否则显示false;
        SparseBooleanArray checkedItemPositions = lvSingleChoice.getCheckedItemPositions();

        // 为防止重复添加, 遍历之前先清空一下
        mValueJsonArray = new JSONArray();

        //循环遍历集合中所有的数据,获取每个item是否在SparseBooleanArray存储,以及对应的值;
        for (int i = 0; i < mValueList.size(); i++) {
            //根据key获取对应的boolean值,没有则返回false
            if (checkedItemPositions.get(i)) {
                mValueJsonArray.put(mValueList.get(i));
            }
            LogUtil.e(TAG, i + " --- " + checkedItemPositions.get(i));
        }
    }
});

猜你喜欢

转载自blog.csdn.net/qq_28261207/article/details/83095851