`
heji
  • 浏览: 87585 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

关于android“多选”的小研究

阅读更多
     转载请注明出处: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"; 

	@Override
	protected 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);
	}

	@Override
	public 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);
			} 
		}
		
		@Override
		public int getCount() {
			return mList.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}
		
		@Override
		public 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?嘿嘿~~~~
   好了,源码我已经贴出来了,希望各路大牛讨论。代码有不对的地方也希望指正,谢谢
~~~~

分享到:
评论
4 楼 guoyu04 2011-02-28  
大哥,你真棒
3 楼 heji 2010-11-05  
很简单啊,你用一个容器存放你所选中的项,为true就放进你的容器里面,为false就不放进去呗,然后再对你的这个容器做相应的操作
2 楼 heji 2010-10-19  
bbzhucheng 写道
这个问题真的很操蛋 我也困扰呢

已经解决了嘛~~~呵呵
1 楼 bbzhucheng 2010-10-12  
这个问题真的很操蛋 我也困扰呢

相关推荐

    网格布局的GridView多选删除功能

    网格布局的GridView多选删除功能,源码gridview,该源码可以支持多选删除功能,而且源码也很简单的,非常方便的,大家可以研究一下吧,片段android源码频道。

    Android应用源码之自定义单选、多选对话框及popwindow窗口实例源码.zip项目安卓应用源码下载

    Android应用源码之自定义单选、多选对话框及popwindow窗口实例源码.zip项目安卓应用源码下载Android应用源码之自定义单选、多选对话框及popwindow窗口实例源码.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 ...

    Android应用源码之自定义本地相册的功能,可以多选图片用.zip项目安卓应用源码下载

    Android应用源码之自定义本地相册的功能,可以多选图片用.zip项目安卓应用源码下载Android应用源码之自定义本地相册的功能,可以多选图片用.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究...

    Android网格布局GridView实现漂亮的多选效果

    但是,如果我们在浏览图片的时候需要一些选中操作、甚至是多选操作的时候。这样的功能我们又该如何实现呢? 可以使用ActionBar +GridView的形式实现!在谈及具体实现之前,首先我们先了解一下什么是 ActionBar: ...

    多选库.zip安卓程序项目源码资源下载

    多选库.zip安卓程序项目源码资源下载多选库.zip安卓程序项目源码资源下载 1.适合学生做毕业设计用 2.适合程序员学习研究用 3.适合小公司换皮做新项目用

    多选库.zip项目安卓应用源码下载

    多选库.zip项目安卓应用源码下载多选库.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考

    实现列表多选的DEMO.zip安卓程序源码资源下载

    实现列表多选的DEMO.zip安卓程序源码资源下载实现列表多选的DEMO.zip安卓程序源码资源下载 1.适合学生做毕业设计用 2.适合程序员学习研究用 3.适合新手自学研究使用

    实现列表多选的DEMO.zip项目安卓应用源码下载

    实现列表多选的DEMO.zip项目安卓应用源码下载实现列表多选的DEMO.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考

    自定义单选、多选对话框及popwindow窗口实例源码.zip安卓程序源码资源下载

    自定义单选、多选对话框及popwindow窗口实例源码.zip安卓程序源码资源下载自定义单选、多选对话框及popwindow窗口实例源码.zip安卓程序源码资源下载 1.适合学生做毕业设计用 2.适合程序员学习研究用 3.适合新手自学...

    自定义本地相册的功能,可以多选图片用.zip安卓程序源码资源下载

    自定义本地相册的功能,可以多选图片用.zip安卓程序源码资源下载自定义本地相册的功能,可以多选图片用.zip安卓程序源码资源下载 1.适合学生做毕业设计用 2.适合程序员学习研究用 3.适合新手自学研究使用

    自定义本地相册的功能,可以多选图片用.zip项目安卓应用源码下载

    自定义本地相册的功能,可以多选图片用.zip项目安卓应用源码下载自定义本地相册的功能,可以多选图片用.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考

    基于Android的文件管理器设计与实现+毕业论文

    可以进行多选操作,可以usb调试,读取手机及sd卡的文件内容。文件管理器需要有搜索动能。 主要研究内容和方法 主要内容包括 1.选择合适的开发语言来进行android开发,和开发环境的配置。 2.对平台界面的设计并完成...

    Android应用源码类似小米的天天文件管理器.zip

    本项目是一个与小米文件管理器类似的小项目,可以分类查看设备...管理文件功能与普通的文件管理器差异不大,可以多选进行文件的删除复制剪切粘贴等操作,比较遗憾的是项目源码没有注释,如果想研究的话只能靠自己捋了,

    相册图片一次性多选

    选中的图片也会以小缩略图的形式在下面显示,如果小缩略图超过3张显示不开右划可以看到其他小缩略图。并且还会在Button按钮上显示选中的图片数量,点击确定可以批量获取选中的图片路径,本例子可以应用的地方很多,...

    文件管理器程序开发研究

    可以进行多选操作,可以usb调试,读取手机及sd卡的文件内容。文件管理器需要有搜索动能。 主要研究内容和方法 主要内容包括 1.选择合适的开发语言来进行android开发,和开发环境的配置。 2.对平台界面的设计并完成...

    Android流式布局FlowLayout详解

    在我们的项目中大部分都是单选效果,为了防止用到多选,demo中也实现了多选; FlowLayout大家不用研究怎么实现的,只要会使用就好; 就好比谷歌提供的ListView条目点击事件一样,只要会用就好,没必要研究个所以然;...

    Android例子源码安卓原生常规对话框演示.zip

    本例子演示了常规的确定取消对话框、pick列表对话框、单选多选对话框、带进度条的对话框等常规原生对话框的用法,需要的朋友可以下载研究一下。项目编码GBK编译版本4.4.2

    Android应用源码类似小米的天天文件管理器

    本项目是一个与小米文件管理器类似的小项目,可以分类查看...管理文件功能与普通的文件管理器差异不大,可以多选进行文件的删除复制剪切粘贴等操作,比较遗憾的是项目源码没有注释,如果想研究的话只能靠自己捋了, 

    类似小米的天天文件管理器

    管理文件功能与普通的文件管理器差异不大,可以多选进行文件的删除复制剪切粘贴等操作,比较遗憾的是项目源码没有注释,如果想研究的话只能靠自己捋了,javaapk之前也发布过很多关于文件管理器的项目,感兴趣的朋友...

    安卓原生常规对话框

    安卓原生常规对话框演示了常规的确定取消对话框、pick列表对话框、单选多选对话框、带进度条的对话框等常规原生对话框的用法,需要的朋友可以下载研究一下。

Global site tag (gtag.js) - Google Analytics