1. 程式人生 > >Android - 單選和多選CheckedTextView

Android - 單選和多選CheckedTextView

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