这几天看了几个google sample发现其中一个以前写过,就是单选和多选的listview,以前写的时候是用的自定义布局判断属性,如果要选中就设置为true,否则设置为false,让后刷新listview,但是demo中不是这么写,图片
public class CheckableLinearLayout extends LinearLayout implements Checkable {
private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked};
private boolean mChecked = false;
public CheckableLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public boolean isChecked() {
return mChecked;
}
public void setChecked(boolean b) {
if (b != mChecked) {
mChecked = b;
refreshDrawableState();
}
}
public void toggle() {
setChecked(!mChecked);
}
@Override
public int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
mergeDrawableStates(drawableState, CHECKED_STATE_SET);
}
return drawableState;
}
}
上边用到了一个函数:refreshDrawableState() 这个函数的作用是:强制view更行他的drawable状态从上面的函数中我们可以看到他会调用view的drawableStateChanged();和viewParent的public void refreshDrawableState() { mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY; drawableStateChanged(); ViewParent parent = mParent; if (parent != null) { parent.childDrawableStateChanged(this); } }
childDrawableStateChanged(View child);来更行drawable状态 onCreateDrawableState(int extraSpace)可以查看CheckBok中的用法........
未选中的点<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="360dp" /> <size android:width="5dp" android:height="5dp" /> <solid android:color="#fff" /> <stroke android:width="1dp" android:color="#000" /> </shape>
选中的点<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="360dp" /> <size android:width="5dp" android:height="5dp" /> <solid android:color="#f00" /> <stroke android:width="1dp" android:color="#000" /> </shape>
selector选择<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/circle" android:state_checked="false" /> <item android:drawable="@drawable/circle_checked" android:state_checked="true" /> </selector>
item<com.example.android.listitemclick.CheckableLinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:minHeight="?android:listPreferredItemHeight" android:orientation="horizontal"> <TextView android:id="@+id/text" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_marginRight="16dp" android:layout_weight="1" android:duplicateParentState="true" /> <ImageView android:id="@+id/image" android:layout_width="20dp" android:layout_height="20dp" android:layout_marginRight="8dp" android:duplicateParentState="true" /*这个要注意必须设置,并且设置为true,设置为true时,<span><span></span></span>*/ android:src="@drawable/ic_hideable_item" />/*他将<span><span>优先从它的直接父控件获得当前的图片状态(例如,focused,state_checked,pressed等等)</span></span>*/ </com.example.android.listitemclick.CheckableLinearLayout>
mainacitivity.xml<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.android.listitemclick.MainActivity"> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:choiceMode="multipleChoice" /> </RelativeLayout>
<!--android:choiceMode="multipleChoice" 这个属性必须设置,他是设置listview的单选,多选,不选....-->
上面代码修改了一小部分,稍微和sample中有点差异
本例来自