转载请注明出处:http://heji.iteye.com/blog/731310

android里面自带有多选的布局,文件名是:simple_list_item_multiple_choice.xml,它的源码如下:
<?xml version="1.0" encoding="utf-8"?><!-- Copyright (C) 2008 The Android Open Source Project     Licensed under the Apache License, Version 2.0 (the "License");     you may not use this file except in compliance with the License.     You may obtain a copy of the License at            http://www.apache.org/licenses/LICENSE-2.0       Unless required by applicable law or agreed to in writing, software     distributed under the License is distributed on an "AS IS" BASIS,     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.     See the License for the specific language governing permissions and     limitations under the License.--><CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@android:id/text1"    android:layout_width="match_parent"    android:layout_height="?android:attr/listPreferredItemHeight"    android:textAppearance="?android:attr/textAppearanceLarge"    android:gravity="center_vertical"    android:checkMark="?android:attr/listChoiceIndicatorMultiple"    android:paddingLeft="6dip"    android:paddingRight="6dip"/>

是一个CheckedTextView的组件。只能实现一个TextView和一个CheckBox的组件。在开发中肯定是不能满足我们的需求的。貌似与它绑定的好像只有ArrayAdapter。什么SimpleAdapter,SimpleCursorAdapter这些Adapter不能与之绑定,看看构造函数就知道了。怎么才能更佳美化我们的ListView的UI呢?只有一个办法,重写一个Adapter,来适配我们的自己的ListView。
下面这个是需要现实在ListView中的布局文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout android:id="@+id/row"xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="wrap_content"><RelativeLayout android:layout_width="fill_parent"android:layout_height="wrap_content"><ImageView android:id="@+id/tag"    android:layout_width="wrap_content" android:layout_height="wrap_content"android:background="@drawable/icon"/><LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_marginLeft="5dip"android:orientation="vertical"android:layout_marginTop="7dip"android:layout_toRightOf="@id/tag"><TextView android:id="@+id/multiple_title"android:layout_width="fill_parent" android:layout_height="wrap_content"    android:gravity="center_vertical"android:textSize="20dip"android:layout_marginLeft="5dip"/><TextView android:id="@+id/multiple_summary"     android:layout_width="fill_parent"android:layout_height="wrap_content" android:layout_marginLeft="5dip"android:gravity="center_vertical"/></LinearLayout><!-- 这三个很重要android:focusable="false"android:focusableInTouchMode="false"android:clickable="false" --><CheckBox          android:id="@+id/multiple_checkbox"        android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:layout_marginTop="6dip"android:focusable="false"android:focusableInTouchMode="false"android:clickable="false"android:layout_alignParentRight="true"/></RelativeLayout>    </LinearLayout>

这个文件是Activity的布局文件:
<?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"><ListView android:id="@+id/listview"android:layout_width="fill_parent"android:layout_height="wrap_content"android:cacheColorHint="#00000000" /></LinearLayout>

有了上面的两个文件,就可以写Activity了~~~~
下面的是源码:
package com.heji.demo.effect;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.CheckBox;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.TextView;import android.widget.AdapterView.OnItemClickListener;import com.heji.demo.R;public class MultipleChoiceActivity extends Activity implements OnItemClickListener{private MyAdapter mSimpleAdapter;public final static String NAME = "name";  public final static String PHONE_NUMBER = "phone"; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setTitle("带有多选的Listview");setContentView(R.layout.multiple_checkbox_main);ListView listView = (ListView) findViewById(R.id.listview);ArrayList<Map<String, String>> al = new ArrayList<Map<String,String>>();Map<String, String> map1 = new HashMap<String, String>();map1.put(NAME, "A");map1.put(PHONE_NUMBER, "132456789");al.add(map1);Map<String, String> map2 = new HashMap<String, String>();map2.put(NAME, "B");map2.put(PHONE_NUMBER, "132134");al.add(map2);Map<String, String> map3 = new HashMap<String, String>();map3.put(NAME, "C");map3.put(PHONE_NUMBER, "132134");al.add(map3);Map<String, String> map4 = new HashMap<String, String>();map4.put(NAME, "D");map4.put(PHONE_NUMBER, "132134");al.add(map4);Map<String, String> map5 = new HashMap<String, String>();map5.put(NAME, "E");map5.put(PHONE_NUMBER, "132134");al.add(map5);Map<String, String> map6 = new HashMap<String, String>();map6.put(NAME, "F");map6.put(PHONE_NUMBER, "132134");al.add(map6);Map<String, String> map7 = new HashMap<String, String>();map7.put(NAME, "G");map7.put(PHONE_NUMBER, "132134");al.add(map7);Map<String, String> map8 = new HashMap<String, String>();map8.put(NAME, "H");map8.put(PHONE_NUMBER, "132134");al.add(map8);Map<String, String> map9 = new HashMap<String, String>();map9.put(NAME, "I");map9.put(PHONE_NUMBER, "132134");al.add(map9);Map<String, String> map10 = new HashMap<String, String>();map10.put(NAME, "J");map10.put(PHONE_NUMBER, "132134");al.add(map10);Map<String, String> map11 = new HashMap<String, String>();map11.put(NAME, "K");map11.put(PHONE_NUMBER, "132134");al.add(map11);String[] from = { NAME, PHONE_NUMBER };int[] to = { R.id.multiple_title, R.id.multiple_summary };mSimpleAdapter = new MyAdapter(this, al, R.layout.multiple_checkbox_main_row, from, to);listView.setAdapter(mSimpleAdapter);listView.setOnItemClickListener(this);}@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {CheckBox checkBox = (CheckBox) view.findViewById(R.id.multiple_checkbox);checkBox.toggle();mSimpleAdapter.map.put(position, checkBox.isChecked());}public class MyAdapter extends SimpleAdapter {Map<Integer, Boolean> map; LayoutInflater mInflater;private List<? extends Map<String, ?>> mList;public MyAdapter(Context context, List<Map<String, String>> data,int resource, String[] from, int[] to) {super(context, data, resource, from, to);map = new HashMap<Integer, Boolean>();mInflater = LayoutInflater.from(context);mList = data;for(int i = 0; i < data.size(); i++) {map.put(i, false);} }@Overridepublic int getCount() {return mList.size();}@Overridepublic Object getItem(int position) {return position;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {if(convertView == null) {convertView = mInflater.inflate(R.layout.multiple_checkbox_main_row, null);}TextView tN = (TextView) convertView.findViewById(R.id.multiple_title);tN.setText((String)mList.get(position).get(NAME));TextView tP = (TextView) convertView.findViewById(R.id.multiple_summary);tP.setText((String)mList.get(position).get(PHONE_NUMBER));CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.multiple_checkbox);checkBox.setChecked(map.get(position)); return convertView;}}}

下面我来分析为什么要用map来保存checkbox的状态。
这个与ListView的刷新机制有关,当你的listview对象很多的时候,每次你拖动listview上下滚动,listview都会刷新一次。怎么刷新呢?比如一个屏幕它最多只显示七条listview,如果你有十条数据,当你想看第八条时,第一条数据理所当然的要被隐藏掉,而第八条数据会被显示,这时listview就刷新了。如果你不保存你所选的checkbox的状态,这时如果你选的是第一条的checkbox的状态为true,当你把余下的第八、第九、第十条数据显示出来时,第十条的checkbox的状态会显示为true,但是它的状态没有被保存,只是你看到它是被选中了而已,其实你选的还是第一条数据。这个问题很操蛋。还有一个更离奇的状态,你让checkbox的状态为true,数据一定要大于十条,你不停的上下拖动屏幕,你会看见checkbox的显示状态会乱跳,但是你实际上选择的还是第一条数据,只是会让你的用户感觉很不爽罢了。难道这个就是android的一个小bug?嘿嘿~~~~
好了,源码我已经贴出来了,希望各路大牛讨论。代码有不对的地方也希望指正,谢谢
~~~~

更多相关文章

  1. Android 系统状态栏一体化
  2. Android系统源码数据库(mmssms.db)
  3. android数据存储之File
  4. Android Studio项目应该提交哪些文件到GitHub上
  5. Android下文件操作模式(含SDCard的读写)
  6. JNI基础实验一:调用.so文件--友善之臂Tiny210 android 串口/pwm/A
  7. Android媒体文件metadata是如何实现跟解码器结合获取的
  8. 【Android】Android清除本地数据缓存代码

随机推荐

  1. 读取android手机流量信息
  2. Android(安卓)SDK 安装中组件的离线安装
  3. Android学习笔记:androidmanifest.xml 高
  4. android 中的 Bitmap 相关
  5. Android剪切板(ClipboardManager)复制文
  6. 面向忙碌开发者的 Android(安卓)知识点收
  7. Smalidea+IntelliJ IDEA/Android(安卓)St
  8. Android的listview、gridview 笔记
  9. Android(安卓)混淆提示 java.io.FileNotF
  10. Android(安卓)market 发布个人免费应用的