通过重写onCreateDrawableState和implements Checkable实现listview的多选和单选

这几天看了几个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状态
    public void refreshDrawableState() {
        mPrivateFlags |= PFLAG_DRAWABLE_STATE_DIRTY;
        drawableStateChanged();

        ViewParent parent = mParent;
        if (parent != null) {
            parent.childDrawableStateChanged(this);
        }
    }
从上面的函数中我们可以看到他会调用view的drawableStateChanged();和viewParent的
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中有点差异

本例来自
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值