标签:setchecked state_checked oncreatedrawablestat mergedrawablestates setitemchecked
activity_main.xml()
<FrameLayout 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" tools:context="com.harvic.checkableimageview.MainActivity" > <ListView android: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_gravity="bottom" android:layout_marginBottom="50dip" 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>
<?xml version="1.0" encoding="utf-8"?> <com.harvic.checkableimageview.CheckableFrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="68dp" > <com.harvic.checkableimageview.CheckableImageView android:layout_gravity="right|center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="10dp" android:clickable="false" android:src="@drawable/toggle" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="17dp" android:layout_marginTop="17dp" android:orientation="vertical" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" /> <TextView android:id="@+id/subtitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="12sp" /> </LinearLayout> </com.harvic.checkableimageview.CheckableFrameLayout>
<?xml version="1.0" encoding="utf-8"?> <selector android:constantSize="true" android:variablePadding="false" xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/toggle_on" android:state_checked="true"/> <item android:drawable="@drawable/toggle_off" android:state_checked="false"/> <item android:drawable="@drawable/toggle_off"/> </selector>
package com.harvic.checkableimageview; public class DataHolder{ public String titleStr; public String subTitleStr; public DataHolder(String title,String subTitle){ titleStr = title; subTitleStr = subTitle; } }
package com.harvic.checkableimageview; 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; // @Override // public void toggle() { // setChecked(!mChecked); // } // // @Override // public boolean isChecked() { // return mChecked; // } @Override public 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); } } } } @Override public boolean isChecked() { // TODO Auto-generated method stub return false; } @Override public void toggle() { // TODO Auto-generated method stub } }
package com.harvic.checkableimageview; import android.content.Context; import android.util.AttributeSet; import android.widget.Checkable; import android.widget.ImageView; /** * extraSpace如果不为0,多余的位置就可以用来放置自定义的状态,使该Drawable具有我们添加的属性 setChecked -> * refreshDrawableState ->onCreateDrawableState ->调用selector设置状态 * 派生自Checkable接口,使CheckableImageView具有的可选中的状态 * ,但当用户点击选中时,我们感觉会调用selector的state_checked="true"的状态, * 但state_checked属性对ImageView是无效的 * ,因为ImageView并不具有可选中的属性,所以我们在调用selector前,应该为ImageView添加上state_checked属性 * 这就是重写onCreateDrawableState * ()的原因,当用户选中时,给ImageView设置选中状态,强制使它处于选中状态中,这时,它就会调用我们的state_checked属性 * */ public class CheckableImageView extends ImageView implements Checkable { public CheckableImageView(Context context, AttributeSet attrs) { super(context, attrs); } private boolean mChecked = false; // <attr name="state_checked" format="boolean"/> private static final int[] CHECKED_STATE_SET = { android.R.attr.state_checked }; /** * 返回值:如果extraSpace为0,直接返回控件本身所具有的状态集。如果extraSpace不为0,返回的值除了具有控件本身所具有的状态集, * 会额外分配指定的空间以便用户自主指定状态。 */ @Override public int[] onCreateDrawableState(int extraSpace) { final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); if (mChecked) { mergeDrawableStates(drawableState, CHECKED_STATE_SET); } return drawableState; } @Override public void setChecked(boolean checked) { if (mChecked != checked) { mChecked = checked; refreshDrawableState(); } } @Override public boolean isChecked() { return false; } @Override public void toggle() { } // @Override // public boolean isChecked() { // return mChecked; // } // // @Override // public void toggle() { // setChecked(!mChecked); // } }
package com.harvic.checkableimageview; import java.util.ArrayList; import java.util.List; import com.example.trylistviewcheckbox.R; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ListView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 构造数据 final List<DataHolder> dataList = new ArrayList<DataHolder>(); for (int i = 0; i < 10; i++) { dataList.add(new DataHolder("harvic的blog------" + i, "harvic")); } // 构造Adapter ListitemAdapter adapter = new ListitemAdapter(MainActivity.this, dataList); final ListView listView = (ListView) findViewById(R.id.list); listView.setAdapter(adapter); // 全部选中按钮的处理 Button all_sel = (Button) findViewById(R.id.all_sel); Button all_unsel = (Button) findViewById(R.id.all_unsel); all_sel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub for (int i = 0; i < dataList.size(); i++) { listView.setItemChecked(i, true); } } }); // 全部取消按钮处理 all_unsel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub for (int i = 0; i < dataList.size(); i++) { listView.setItemChecked(i, false); } } }); } }
package com.harvic.checkableimageview; import java.util.List; import com.example.trylistviewcheckbox.R; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class ListitemAdapter extends BaseAdapter { private List<DataHolder> mList; private Context mContext; private LayoutInflater mInflater; public ListitemAdapter(Context context,List<DataHolder> list){ mList = list; mContext = context; mInflater = LayoutInflater.from(context); } @Override public int getCount() { // TODO Auto-generated method stub return mList.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return mList.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder = null; if (convertView == null) { holder=new ViewHolder(); convertView = mInflater.inflate(R.layout.check_list_item, null); holder.mTitle = (TextView)convertView.findViewById(R.id.title); holder.mSubTitile = (TextView)convertView.findViewById(R.id.subtitle); convertView.setTag(holder); }else { holder = (ViewHolder)convertView.getTag(); } holder.mTitle.setText((String)mList.get(position).titleStr); holder.mSubTitile.setText((String)mList.get(position).subTitleStr); return convertView; } public class ViewHolder{ public TextView mTitle; public TextView mSubTitile; }; }
标签:setchecked state_checked oncreatedrawablestat mergedrawablestates setitemchecked
原文地址:http://blog.csdn.net/u013210620/article/details/47807157