list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_margin="5dp" /> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/img"
android:orientation="vertical" > <TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="22sp" /> <TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="13sp" />
</LinearLayout> <CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false" /> </RelativeLayout>

listview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消" /> <Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="反选" /> <TextView
android:id="@+id/totalTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="共选了多少个" />
</LinearLayout> <ListView
android:id="@+id/mylistview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:divider="@android:color/darker_gray"
android:dividerHeight="1dp"
android:fadingEdge="none"
android:scrollingCache="false" >
</ListView> </LinearLayout>

ViewHolder.java

package com.wangzhu.demoselector;

import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView; public final class ViewHolder { public ImageView img;
public TextView title;
public TextView info;
public CheckBox checkBox;
}

MyAdapter.java

package com.wangzhu.demoselector;

import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.annotation.SuppressLint;
import android.content.Context;
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.ImageView;
import android.widget.TextView; public class MyAdapter extends BaseAdapter { private Context context;
private LayoutInflater inflater; private List<Map<String, Object>> items;
private Map<Integer, Boolean> isSelectedMap; public MyAdapter(Context context, List<Map<String, Object>> items) {
this.context = context;
this.items = items;
init();
} private void init() {
Log.d("MyAdapter", "MyAdapter init");
inflater = LayoutInflater.from(context);
isSelectedMap = new HashMap<Integer, Boolean>();
for (int i = 0, size = items.size(); i < size; i++) {
isSelectedMap.put(i, false);
}
} @Override
public int getCount() {
return items.size();
} @Override
public Object getItem(int position) {
return items.get(position);
} @Override
public long getItemId(int position) {
return position;
} @SuppressLint("NewApi")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (null == convertView) {
convertView = inflater.inflate(R.layout.list_item, null);
// convertView = inflater.inflate(R.layout.list_item_1, null);
viewHolder = new ViewHolder();
viewHolder.img = (ImageView) convertView.findViewById(R.id.img);
viewHolder.title = (TextView) convertView.findViewById(R.id.title);
viewHolder.info = (TextView) convertView.findViewById(R.id.info);
viewHolder.checkBox = (CheckBox) convertView
.findViewById(R.id.check);
convertView.setTag(viewHolder); } else {
viewHolder = (ViewHolder) convertView.getTag();
} viewHolder.img.setBackgroundResource(Integer.parseInt(String
.valueOf(items.get(position).get("img"))));
viewHolder.title.setText(String.valueOf(items.get(position)
.get("title")));
viewHolder.info
.setText(String.valueOf(items.get(position).get("info"))); // 或者在list_item.xml中设置
// android:background="@drawable/list_item_bg_selector" Log.d("MyAdapter",
"1===getView isSelected===" + convertView.isSelected()
+ " isChecked======"
+ viewHolder.checkBox.isChecked()); viewHolder.checkBox.setChecked(isSelectedMap.get(position));
convertView.setPressed(isSelectedMap.get(position));
convertView.setSelected(isSelectedMap.get(position));
convertView.setActivated(isSelectedMap.get(position)); Log.d("MyAdapter",
"2====getView isSelected===" + convertView.isSelected()
+ " isChecked======"
+ viewHolder.checkBox.isChecked()); // convertView
// .setBackgroundResource(R.drawable.list_item_bg_color_selector);
// convertView.setBackgroundResource(R.drawable.list_item_bg_selector); if (isSelectedMap.get(position)) {
convertView.setBackgroundResource(R.drawable.child_list_bg_1);
} else {
convertView.setBackgroundResource(R.drawable.child_list_item);
} return convertView;
} public Map<Integer, Boolean> getIsSelectedMap() {
return isSelectedMap;
} }

ListViewActivity.java

package com.wangzhu.demoselector;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView; public class ListViewActivity extends Activity implements OnClickListener { private final static String TAG = "ListViewActivity";
private MyAdapter myAdapter;
private ListView listView;
private Button button1, button2, button3;
private TextView totalTv; private List<Map<String, Object>> datas; private int count = 0; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
init();
} private void init() {
getDatas(); button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(this); button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this); button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(this); totalTv = (TextView) findViewById(R.id.totalTv); myAdapter = new MyAdapter(getApplicationContext(), datas);
listView = (ListView) findViewById(R.id.mylistview);
listView.setAdapter(myAdapter);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);// 多选模式
// listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);// 单选模式 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.d(TAG,
"onItemClick======="
+ String.valueOf(datas.get(position).get(
"title")) + " position=" + position
+ " id=" + id);
ViewHolder viewHolder = (ViewHolder) view.getTag();
CheckBox checkBox = viewHolder.checkBox;
final Map<Integer, Boolean> isSelectedMap = myAdapter
.getIsSelectedMap();
switch (listView.getChoiceMode()) {
case ListView.CHOICE_MODE_SINGLE:
if (!isSelectedMap.get(position)) {
checkBox.toggle();
isSelectedMap.put(position, true);
}
break;
case ListView.CHOICE_MODE_MULTIPLE:
checkBox.toggle();
if (checkBox.isChecked()) {
count++;
} else {
count--;
} handler.sendEmptyMessage(0); if (checkBox.isChecked()) {
view.setBackgroundResource(R.drawable.child_list_bg_1);
} else {
view.setBackgroundResource(R.drawable.child_list_item);
}
isSelectedMap.put(position, checkBox.isChecked());
break;
default:
break;
}
}
});
} public void getDatas() {
datas = new ArrayList<Map<String, Object>>();
Map<String, Object> map = null;
for (int i = 0; i < 20; i++) {
map = new HashMap<String, Object>();
map.put("title", "G" + i);
map.put("info", "Google " + i);
map.put("img", R.drawable.ic_launcher);
datas.add(map);
}
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
allSelected();
break;
case R.id.button2:
cancelSelected();
break;
case R.id.button3:
ReverseSelection();
break;
default:
break;
}
} /**
* 全选
*/
public void allSelected() {
final Map<Integer, Boolean> isSelectedMap = myAdapter
.getIsSelectedMap();
for (int i = 0, size = isSelectedMap.size(); i < size; i++) {
if (!isSelectedMap.get(i)) {
count++;
}
isSelectedMap.put(i, true);
}
dataChanged();
} /**
* 取消
*/
public void cancelSelected() {
final Map<Integer, Boolean> isSelectedMap = myAdapter
.getIsSelectedMap();
for (int i = 0, size = isSelectedMap.size(); i < size; i++) {
if (isSelectedMap.get(i)) {
count--;
}
isSelectedMap.put(i, false);
}
dataChanged();
} /**
* 反选
*/
public void ReverseSelection() {
final Map<Integer, Boolean> isSelectedMap = myAdapter
.getIsSelectedMap();
for (int i = 0, size = isSelectedMap.size(); i < size; i++) {
// isSelectedMap.put(i, !isSelectedMap.get(i));
if (isSelectedMap.get(i)) {
count--;
isSelectedMap.put(i, false);
} else {
count++;
isSelectedMap.put(i, true);
} }
dataChanged();
} public void dataChanged() {
// 自定义的BaseAdapter中调用notifyDataSetChanged方法会重新调用BaseAdapter的getView方法
myAdapter.notifyDataSetChanged();
handler.sendEmptyMessage(0);
} public Handler handler = new Handler() { @Override
public void handleMessage(Message msg) {
totalTv.setText("选中了" + count + "个");
} };
}

以下是本次Demo中用到的图片:

备注:欢迎各位大牛多多指教,谢谢!

Android ListView(Selector 背景图片 全选 Checkbox等按钮)的更多相关文章

  1. Android ListView批量选择(全选、反选、全不选)

    APP的开发中,会常遇到这样的需求:批量取消(删除)List中的数据.这就要求ListVIew支持批量选择.全选.单选等等功能,做一个比较强大的ListView批量选择功能是很有必要的,那如何做呢? ...

  2. Android中selector背景选择器

    http://blog.csdn.net/forsta/article/details/26148403 http://blog.csdn.net/wswqiang/article/details/6 ...

  3. Android的selector 背景选择器

    关于listview和button都要改变android原来控件的背景,在网上查找了一些资料不是很全,所以现在总结一下android的selector的用法.首先android的selector是在d ...

  4. Android ListView的批量处理(多选/反选/删除)

    在Android开发中经常遇到使用ListView的情况,有时候需要的不仅仅是列表显示,还有长按列表进行多选,并且批量删除的情况,在这里记录一下自己的所学. 先上效果图: 几个需要用到的核心方法: / ...

  5. android怎么换背景图片

    我不晓得一般是怎么做的,但是至少可以用两种方法,一种是用一个全屏的ImageView来当作背景,通过修改imageview来修改背景图片,一种是将你xml中最外层的那个布局,LinerLayout之类 ...

  6. Android RadioButton selector背景

    RadioButton selector 背景 <?xml version="1.0" encoding="utf-8"?> <selecto ...

  7. js全选checkbox框

    html: <input  type="checkbox" id="checkbox1" value="1" onclick=&quo ...

  8. winform datagridview在添加全选checkbox时提示:不能设置 selected 或 selected 既不是表 Table 的 DataColumn 也不是 DataRelation。

    在项目中,需要多选功能,于是在datagridview添加了一列DataGridViewCheckBoxColumn 在给datagridview绑定完数据集之后,对全选进行操作的时候,发现总报错,报 ...

  9. Jquery 组 checkbox全选checkbox

    <!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8&qu ...

随机推荐

  1. php入门实现留言板

    首先由一个文本文档read.txt liulan.html <!doctype html> <html lang="en"> <head> &l ...

  2. SQL*PLUS中批量执行SQL语句

    SQL*PLUS中批量执行SQL语句 今天由于工作的需要,要在CMD中批量执行大量的SQL语句,对于Oracle学习还处在入门阶段的我,只能硬着头皮到处去寻找资料(主要是网络资料,也包括自己的电子书) ...

  3. Unity3D 之连按移动加速

    上代码: 效果是连续按W后,加速移动 为物体添加个拖拽效果,方便看运动轨迹. 将下面的脚本绑定到移动的物体上. 不过这里有一点很重要的需要去注意就是该方法不能放在 void FixedUpdate() ...

  4. bootstrap 的 datetimepicker 结束时间大于开始时间

    web的时间js控件,在管理性的项目中频繁用到,总结了一些用到的知识,分享出来,供以后学习: 1.首先引用资源包: bootstrap基础资源包: bootstrap.min.css .bootstr ...

  5. jQuery Easy UI 使用

    一.引入必要文件 二.加载UI组件的方式 加载 UI 组件有两种方式: 1.使用 class 方式加载: 2.使用 JS 调用加载.//使用 class 加载,格式为: easyui-组件名 效果: ...

  6. VS打包

    同学让帮忙打包个VC的程序,程序比较简单,是带access数据库的统计查询软件,之前用Visual Studio 6.0自带的Package & Deployment Wizard 工具打包过 ...

  7. js中的FileSystemObject使用(FSO)

    Set fso = Server.CreateObject("Scripting.FileSystemObject") 定义FSO对象 fso.CreateFolder(Serve ...

  8. nodejs js模块加载

    本文地址:http://www.cnblogs.com/jasonxuli/p/4381747.html nodejs的非核心模块(core module)加载主要使用的就是module.js. 项目 ...

  9. Poj 1001 / OpenJudge 2951 Exponentiation

    1.链接地址: http://poj.org/problem?id=1001 http://bailian.openjudge.cn/practice/2951 2.题目: Exponentiatio ...

  10. linux shell编程学习笔记(一)---通配符,元字符

    linux通配符: 通配符是由shell处理的(不是由所涉及到命令语句处理的,其实我们在shell各个命令中也没有发现有这些通配符介绍), 它只会出现在 命令的“参数”里(它不用在 命令名称里, 也不 ...