码迷,mamicode.com
首页 > 其他好文 > 详细

ListView与CheckBox组合实现单选

时间:2016-05-05 09:40:49      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:

main.xml配置文件
<?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="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    
        <ListView android:id="@+id/lv_single_list"
        android:descendantFocusability="blocksDescendants"
        android:fastScrollEnabled="true" android:clickable="true"
        android:divider="@drawable/divider_horizontal_timeline"
        android:dividerHeight="1.0dip" android:layout_width="fill_parent"
        android:layout_height="400dp" android:scrollingCache="false"
        android:fadingEdge="none" android:cacheColorHint="#00000000" />

</LinearLayout>
适配器adapter的配置文件list_adapter.xml
<?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="fill_parent" android:gravity="center" 
    android:orientation="horizontal">

    <TextView android:id="@+id/tv_zxing_section_sequence"
        android:layout_width="50dp" android:layout_height="wrap_content"
        android:textSize="16sp" />

    <TextView android:id="@+id/tv_zxing_sectionname"
        android:layout_width="210dp" android:layout_height="wrap_content"
        android:textSize="14sp" android:layout_marginLeft="10dp" />
        
    <CheckBox android:id="@+id/item_cb_section" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:focusable="false"
        android:focusableInTouchMode="false" android:clickable="false"
        android:layout_alignParentTop="true" android:layout_marginLeft="5dp" />

</LinearLayout>
    
SingleListChoiceActivity.java

package com.amker.test;

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.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

public class SingleListChoiceActivity extends Activity {

    private ListView listView;
    
    private Map<Integer, Boolean> isSelected;
    
    private List beSelectedData = new ArrayList();    
    
    ListAdapter adapter;
    
    private List cs = null;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        listView = (ListView) this.findViewById(R.id.lv_single_list);
        cs  = new ArrayList();
        cs.add("aaaaaa");
        cs.add("bbbbbb");
        cs.add("cccccc");
        cs.add("dddddd");
        cs.add("eeeeee");
        cs.add("ffffff");
        cs.add("gggggg");
        cs.add("hhhhhh");
        cs.add("jjjjjj");
        
        initList();
    }
    
    void initList(){
        
        if (cs == null || cs.size() == 0)
            return;
        if (isSelected != null)
            isSelected = null;
        isSelected = new HashMap<Integer, Boolean>();
        for (int i = 0; i < cs.size(); i++) {
            isSelected.put(i, false);
        }
        // 清除已经选择的项
        if (beSelectedData.size() > 0) {
            beSelectedData.clear();
        }
        adapter = new ListAdapter(this, cs);
        listView.setAdapter(adapter);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        adapter.notifyDataSetChanged();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
               Log.i("map", cs.get(position).toString());
            }
        });
        
    }
    
    class ListAdapter extends BaseAdapter {

        private Context context;

        private List cs;

        private LayoutInflater inflater;

        public ListAdapter(Context context, List data) {
            this.context = context;
            this.cs = data;
            initLayoutInflater();
        }

        void initLayoutInflater() {
            inflater = LayoutInflater.from(context);
        }

        public int getCount() {
            return cs.size();
        }

        public Object getItem(int position) {
            return cs.get(position);
        }

        public long getItemId(int position) {
            return 0;
        }

        public View getView(int position1, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            View view = null;
            final int position = position1;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.list_adapter,
                        null);
                holder = new ViewHolder();
                holder.checkBox = (CheckBox) convertView
                        .findViewById(R.id.item_cb_section);
                holder.tv_sequence = (TextView) convertView
                        .findViewById(R.id.tv_zxing_section_sequence);
                holder.tv_sectionname = (TextView) convertView
                        .findViewById(R.id.tv_zxing_sectionname);
                convertView.setTag(holder);
            } else {
                view = convertView;
                holder = (ViewHolder) view.getTag();
            }            
            holder.checkBox.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    // 当前点击的CB
                    boolean cu = !isSelected.get(position);
                    // 先将所有的置为FALSE
                    for(Integer p : isSelected.keySet()) {
                        isSelected.put(p, false);
                    }
                    // 再将当前选择CB的实际状态
                    isSelected.put(position, cu);
                    ListAdapter.this.notifyDataSetChanged();
                    beSelectedData.clear();
                    if(cu) beSelectedData.add(cs.get(position));
                }
            });
            holder.tv_sequence.setText(String.valueOf(position + 1));
            holder.tv_sectionname.setText(cs.get(position).toString());
            holder.checkBox.setChecked(isSelected.get(position));
            return convertView;
        }
    }
    
    class ViewHolder {

        CheckBox checkBox;

        TextView tv_sequence;

        TextView tv_sectionname;

    }
}

 

ListView与CheckBox组合实现单选

标签:

原文地址:http://www.cnblogs.com/zhujiabin/p/5460499.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!