带checkbox的ListView实现(二)

同样,还是先给大家看看效果图:(与上篇的效果一样,其实我就是用的上一篇的效果图)

同样,我们还是从布局开始讲。

一、activity_main.xml ——MainActivity布局文件<FrameLayout xmlns:android=""xmlns:tools=""android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.harvic.trylistviewcheckbox.MainActivity" ><ListViewandroid:id="@+id/list"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="100dp"android:choiceMode="multipleChoice"android:dividerHeight="1px"android:scrollbars="none" /><Button android:id="@+id/all_sel"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginBottom="50dip"android:layout_gravity="bottom"android:text="全选"/><Button android:id="@+id/all_unsel"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_gravity="bottom"android:text="全部取消"/></FrameLayout>注意啦:

这里的布局是与上一篇一样一样的,一个ListView两个按钮。但这里有个参数必须要添加:

在ListView中:

android:choiceMode="multipleChoice"这个很重要,android:choiceMode中有三个取值:none、singleChoice、multipleChoice;分别代表:不可选择,单选和多选。在这里,不同的取值对ListView的影响是不一样的,但这个影响仅对ListView的Item自己带有checkable属性时才起作用,对于没有checkable属性的ListView是没有任何效果的,比如,你把android:choiceMode这个参数放在上篇的代码中,无论你取什么值都没有任何作用,具体为什么,下面再讲。二、check_list_item.xml —— 单个Item布局<?xml version="1.0" encoding="utf-8"?><com.harvic.trylistviewcheckbox.CheckableFrameLayout xmlns:android=""android:layout_width="match_parent"android:layout_height="68dp"><CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="right|center_vertical"android:button="@drawable/checkbox_selector"android:clickable="false"android:focusable="false" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginBottom="17dp"android:layout_marginTop="17dp"android:orientation="vertical"><TextViewandroid:id="@+id/title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="16sp" /><TextViewandroid:id="@+id/subtitle"android:textSize="12sp"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout></com.harvic.trylistviewcheckbox.CheckableFrameLayout>这里与上一篇有两个地方不同:

1、很明显,使用的自定义控件CheckableFrameLayout;

2、CheckBox控件没有定义ID值,如果根据上一篇的方式,大家可能会打个问号,没有ID怎么获取这个控件,又怎么让它显示选中与不选中呢。下面看看自定义的控件类CheckableFrameLayout的实现:

package com.harvic.trylistviewcheckbox;import android.content.Context;import android.util.AttributeSet;import android.view.View;import android.widget.Checkable;import android.widget.FrameLayout;public class CheckableFrameLayout extends FrameLayout implements Checkable {public CheckableFrameLayout(Context context, AttributeSet attrs) {super(context, attrs);}private boolean mChecked = false;@Overridepublic void toggle() {setChecked(!mChecked);}@Overridepublic boolean isChecked() {return mChecked;}@Overridepublic void setChecked(boolean checked) {if (mChecked != checked) {mChecked = checked;refreshDrawableState();for (int i = 0, len = getChildCount(); i < len; i++) {View child = getChildAt(i);if(child instanceof Checkable){((Checkable) child).setChecked(checked);}}}}}因为我们用到的是FrameLayout控件,所以这里派生自FrameLayout,,如果用到诸如LinearLayout,RelativeLayout控件等,就要改成对应的类。最后还继承了Checkable接口;

FrameLayout没什么好讲的,就是帧布局。关键问题在于Checkable接口,凡实现这个接口的控件都将具有可选中状态的属性;在普通控件中,具有这个状态的有:CheckBox, CheckedTextView, CompoundButton, RadioButton, ToggleButton等。(《Android 中文API (33) —— Checkable》)对于如何展示它的选中状态需要用户自主实现三个函数:isChecked(),toggle(),setChecked();也就是说,当用户使用ListView.setItemChecked(int position,bool checked)函数来将指定的Item设为Checked标识时,就会自动调用这里的SetChecked()函数。在代码中,我们如何设计,用户界面就会显示怎样的选中状态;

没有什么可留恋,只有抑制不住的梦想,

带checkbox的ListView实现(二)

相关文章:

你感兴趣的文章:

标签云: