1
回答
通过重写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的显示状态会乱跳,但是你实际上选择的还是第一条数据,只是会让你的用户感觉很不爽罢了。

举报
鉴客
发帖于6年前 1回/515阅
顶部