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中用到的图片:
备注:欢迎各位大牛多多指教,谢谢!