布局文件

XML code
                                                         <?                    xml version="1.0" encoding="utf-8"                    ?>                                         <                    LinearLayout                     xmlns:android                    ="http://schemas.android.com/apk/res/android"                     android:layout_width                    ="fill_parent"                     android:layout_height                    ="wrap_content"                     android:orientation                    ="horizontal"                                         >                                         <                    RelativeLayout                     android:id                    ="@+id/RelativeLayout01"                     android:layout_width                    ="fill_parent"                     android:layout_height                    ="wrap_content"                     android:gravity                    ="center_horizontal"                                         >                                         <                    ImageView                     android:id                    ="@+id/ImageView01_checklayout"                     android:layout_width                    ="wrap_content"                     android:layout_height                    ="wrap_content"                     android:layout_alignParentLeft                    ="true"                                         >                                         </                    ImageView                    >                                         <                    TextView                     android:id                    ="@+id/TextView01_checklayout"                     android:layout_width                    ="wrap_content"                     android:layout_height                    ="wrap_content"                     android:layout_marginTop                    ="20px"                     android:layout_marginLeft                    ="70px"                     android:textSize                    ="20px"                     android:gravity                    ="center"                                         >                                         </                    TextView                    >                                         <                    CheckBox                     android:id                    ="@+id/CheckBox01_checklayout"                     android:layout_width                    ="wrap_content"                     android:layout_height                    ="wrap_content"                     android:layout_alignParentRight                    ="true"                     android:layout_marginTop                    ="14px"                                         ></                    CheckBox                    >                                         </                    RelativeLayout                    >                                         </                    LinearLayout                    >                                     
                    
Java code
                                                                                             package                                 com.jingoal.mobile.android.jingoal.adapter;                                 import                                 java.util.ArrayList;                                 import                                 java.util.List;                                 import                                 com.jingoal.mobile.android.jingoal.R;                                 import                                 com.jingoal.mobile.android.jingoal.holder.CheckBoxViewHolder;                                 import                                 android.content.Context;                                 import                                 android.graphics.Bitmap;                                 import                                 android.graphics.BitmapFactory;                                 import                                 android.util.Log;                                 import                                 android.view.LayoutInflater;                                 import                                 android.view.View;                                 import                                 android.view.ViewGroup;                                 import                                 android.widget.BaseAdapter;                                 import                                 android.widget.CheckBox;                                 import                                 android.widget.CompoundButton;                                 import                                 android.widget.ImageView;                                 import                                 android.widget.TextView;                                 import                                 android.widget.CompoundButton.OnCheckedChangeListener;                                 /**                                * * 带复选框的listView的适配器 *                                 @author                                 Administrator *                                 */                                                                 public                                                                 class                                 MyCheckBoxAdapter                                 extends                                 BaseAdapter {                                 //                                 用来将布局文件变成列表中的文件                                                                                                  private                                 LayoutInflater mInflater                                 =                                                                 null                                ;                                 private                                 Bitmap mIcon                                 =                                                                 null                                ;                                 private                                 Context myContext                                 =                                                                 null                                ;                                 private                                 List                                <                                People                                >                                 data                                 =                                                                 null                                ;                                 private                                 ArrayList                                <                                People                                >                                 checkeddata                                 =                                                                 new                                 ArrayList                                <                                People                                >                                ();                                 private                                 ArrayList                                <                                CheckBox                                >                                 allCheck                                 =                                                                 new                                 ArrayList                                <                                CheckBox                                >                                ();                                 //                                 是否全选                                                                                                  private                                                                 boolean                                 isSelectAll                                 =                                                                 false                                ;                                 public                                 MyCheckBoxAdapter(Context context, List                                <                                People                                >                                 data) {                                 this                                .myContext                                 =                                 context;                                 this                                .mInflater                                 =                                 LayoutInflater.from(                                this                                .myContext);                                 this                                .mIcon                                 =                                 BitmapFactory.decodeResource(                                 this                                .myContext.getResources(), R.drawable.icon1);                                 this                                .data                                 =                                 data; } @Override                                 public                                                                 int                                 getCount() {                                 return                                 data.size(); } @Override                                 public                                 Object getItem(                                int                                 position) {                                 //                                 TODO Auto-generated method stub                                                                                                  return                                 data.get(position); } @Override                                 public                                                                 long                                 getItemId(                                int                                 position) {                                 //                                 TODO Auto-generated method stub                                                                                                  return                                 position; } @Override                                 public                                 View getView(                                final                                                                 int                                 position, View view, ViewGroup parent) {                                 final                                 CheckBoxViewHolder holder;                                //                                 布局的结构类                                                                                                  if                                 (view                                 ==                                                                 null                                )                                //                                 如果现在没有视图                                                                  {                                 //                                 初始化布局文件和布局的组建结构                                                                  view                                 =                                                                 this                                .mInflater.inflate(R.layout.check_layout,                                 null                                ); holder                                 =                                                                 new                                 CheckBoxViewHolder(); holder.mImageView                                 =                                 (ImageView) view.findViewById(R.id.ImageView01_checklayout); holder.mTextView                                 =                                 (TextView) view.findViewById(R.id.TextView01_checklayout); holder.mCheckBox                                 =                                 (CheckBox) view.findViewById(R.id.CheckBox01_checklayout);                                 //                                 设置是否被全选                                                                                                  if                                 (                                this                                .isSelectAll                                 ==                                                                 true                                ) { holder.mCheckBox.setChecked(                                true                                ); }                                 else                                 { holder.mCheckBox.setChecked(                                false                                ); } view.setTag(holder);                                //                                 将布局指向一个模式                                                                  }                                 else                                 { holder                                 =                                 (CheckBoxViewHolder) view.getTag(); }                                 //                                 设置布局里的组建元素                                                                  holder.mTextView.setText(data.get(position).toString()); holder.mImageView.setImageBitmap(mIcon);                                 if                                 (holder.mCheckBox.isChecked()) {                                 if                                 (                                this                                .checkeddata.size()                                 <                                                                 0                                ) {                                 this                                .checkeddata.add(                                this                                .data.get(position)); }                                 else                                 {                                 if                                 (                                this                                .checkeddata.indexOf(                                this                                .data.get(position))                                 <                                                                 0                                ) {                                 this                                .checkeddata.add(                                this                                .data.get(position)); } } } holder.mCheckBox .setOnCheckedChangeListener(                                new                                 OnCheckedChangeListener() { @Override                                 public                                                                 void                                 onCheckedChanged(CompoundButton button,                                 boolean                                 isSelected) { ArrayList                                <                                People                                >                                 list                                 =                                 MyCheckBoxAdapter.                                this                                .checkeddata;                                 if                                 (isSelected                                 ==                                                                 true                                ) {                                 if                                 (list.size()                                 >                                                                 0                                ) { People people                                 =                                 MyCheckBoxAdapter.                                this                                .data .get(position);                                 if                                 (list.indexOf(people)                                 <                                                                 0                                ) { list.add(people); } }                                 else                                 { list.add(MyCheckBoxAdapter.                                this                                .data .get(position)); } }                                 else                                 {                                 if                                 (list.size()                                 >                                                                 0                                ) { People people                                 =                                 MyCheckBoxAdapter.                                this                                .data .get(position);                                 for                                 (                                int                                 i                                 =                                                                 0                                ; i                                 <                                 list.size(); i                                ++                                ) {                                 if                                 (people                                 ==                                 list.get(i)) { list.remove(i); } } } } } });                                 return                                 view; }                                 /**                                 * 得到已选择的条数 *                                 @return                                                                 */                                                                 public                                                                 int                                 getCheckedSize() {                                 return                                                                 this                                .checkeddata.size(); }                                 /**                                 * 获得选择的数据列表 *                                 @return                                                                 */                                                                 public                                 ArrayList                                <                                People                                >                                 getChecked() {                                 return                                                                 this                                .checkeddata; }                                 /**                                 * 是否全选 *                                 @return                                                                 */                                                                 public                                                                 boolean                                 isSelectAll() {                                 return                                 isSelectAll; }                                 /**                                 * 清除所有的选项                                 */                                                                 public                                                                 void                                 removeAllChecked() {                                 int                                 size                                 =                                 getCheckedSize();                                 for                                (                                int                                 i                                 =                                                                 0                                 ; i                                 <                                 size ; i                                ++                                ) {                                 this                                .checkeddata.removeAll(checkeddata); } }                                 /**                                 * 设置全选 *                                 @param                                 isSelectAll                                 */                                                                 public                                                                 void                                 setSelectAll(                                boolean                                 isSelectAll) {                                 this                                .isSelectAll                                 =                                 isSelectAll; } }                             

更多相关文章

  1. 相机 android java.lang.RuntimeException: setParameters faile
  2. android的文件操作
  3. android GPS定位 经纬度确定
  4. 更新android sdk出现 A folder failed to be renamed or moved
  5. android 外部启动activity,…
  6. android编程中setLayoutParams方法设置
  7. 视频的播放
  8. 向SD卡写文件
  9. NPM 和webpack 的基础使用

随机推荐

  1. 分析php生成短网址/短链接原理和用法实例
  2. 详解VSCode+PHPstudy配置PHP开发环境的步
  3. php array_unshift()函数详解(实例)
  4. 详解PHP论坛实现系统的思路
  5. 值得一看!高级PHP工程师必备的编码技巧及
  6. 实例讲解php提交表单关闭layer弹窗iframe
  7. 控制反转原则,它和依赖注入有什么联系
  8. 教你使用mixphp打造多进程异步邮件发送
  9. 分析ThinkPHP防止重复提交表单的方法实例
  10. 总结PHP实现提取多维数组指定一列的方法