正文  UI设计 > CheckBox/复选框 >

Android listview中添加CheckBox的完美实现

本文实现的功能是将listview子项中的checkbox与listview分离出来,即点击listview的子项实现的是查看listview子项的内容,点击子项中的checkbox来实现checkbox的选中与否本文没有实现listview的分页滚屏效果以下......

本文实现的功能是将listview子项中的checkbox与listview分离出来,即点击listview的子项实现的是查看listview子项的内容,点击子项中的checkbox来实现checkbox的选中与否本文没有实现listview的分页滚屏效果以下是activity的代码:下面还有xml文件

package com.test;

import java.util.ArrayList;

import java.util.HashMap;

import android.app.Activity;

import android.app.AlertDialog;

import android.app.AlertDialog.Builder;

import android.content.Context;

import android.os.Bundle;

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.Button;

import android.widget.CheckBox;

import android.widget.ListView;

import android.widget.TextView;

import android.widget.AdapterView.OnItemClickListener;

 

public class ListView_CheckBoxActivity extends Activity {

    int scroll_num = 10;

    ViewHolder holder;

    HashMap<Integer,Boolean> isSelected;

    MyAdapter adapter;

    ArrayList<HashMap> data;

    HashMap map;

   

    ListView mListView;

    Button btn_all;

    Button btn_none;

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        mListView = (ListView)findViewById(R.id.main_listview);

        btn_all = (Button)findViewById(R.id.main_all);

        btn_none = (Button)findViewById(R.id.main_none);

        //初始化数据

        init_data();

        //设置按钮点击事件

        init_btn();

        //设置listview

        init_listview();

    }

    //初始化数据

    public void init_data(){

    data = new ArrayList<HashMap>();

        for(int i = 0; i<20; i++){

        map = new HashMap();

        map.put("title", "title-->"+i);

        map.put("content", "content-------------------------------->"+i);

        data.add(map);

        }

    }

    //设置按钮点击事件

    public void init_btn(){

    btn_all.setOnClickListener(new OnClickListener() {

           public void onClick(View v) {

              // TODO Auto-generated method stub

              for(int i = 0; i<data.size(); i++){

                  isSelected.put(i, true);

                  adapter.notifyDataSetChanged();

              }

           }

       });

        btn_none.setOnClickListener(new OnClickListener() {

           public void onClick(View v) {

              for(int i = 0; i<data.size(); i++){

                  isSelected.put(i, false);

                  adapter.notifyDataSetChanged();

              }

           }

       });

    }

    //设置listview

    public void init_listview(){

    adapter = new MyAdapter(ListView_CheckBoxActivity.this, data);

        mListView.setAdapter(adapter);

        //为listview的子项添加点击事件,点击后查看短信内容

       mListView.setOnItemClickListener(new OnItemClickListener() {

 

           @Override

           public void onItemClick(AdapterView<?> parent, View view,

                  int position, long id) {

              holder = (ViewHolder) view.getTag();

              AlertDialog.Builder builder = new Builder(ListView_CheckBoxActivity.this);

             

              builder.setTitle(holder.title.getText().toString()+":")

                  .setMessage(holder.content.getText().toString());

              AlertDialog dialog = builder.create();

              dialog.setCanceledOnTouchOutside(true);

              dialog.show();

           }

          

       });

    }

   

    class MyAdapter extends BaseAdapter{

    int count = scroll_num;

    Context mContext;

    ArrayList<HashMap> mData;

    LayoutInflater mInflater;

   

    public MyAdapter(Context context,ArrayList<HashMap> data) {

        this.mContext = context;

        this.mData = data;

        mInflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);

        if(count>mData.size()){

            count = mData.size();

        }

        System.out.println("1111111111");

        isSelected = new HashMap<Integer,Boolean>();

        for(int i = 0; i<data.size(); i++){

            isSelected.put(i, false);

        }

    }

   

    @Override

    public int getCount() {

        return mData.size();

    }

   

    @Override

    public Object getItem(int position) {

        return position;

    }

 

    @Override

    public long getItemId(int position) {

        return position;

    }

 

    @Override

    public View getView(final int position, View convertView, ViewGroup parent) {

        if(convertView == null){

            convertView = mInflater.inflate(R.layout.item_listview, null);

            holder = new ViewHolder();

            holder.title = (TextView) convertView.findViewById(R.id.item_listview_title);

            holder.content = (TextView) convertView.findViewById(R.id.item_listview_content);

            holder.checkBox = (CheckBox) convertView.findViewById(R.id.item_listview_checkbox);

            convertView.setTag(holder);

        }else{

            holder = (ViewHolder) convertView.getTag();

        }

       

            holder.title.setText(data.get(position).get("title").toString());

            holder.content.setText(data.get(position).get("content").toString());

            holder.checkBox.setChecked(isSelected.get(position));

            holder.checkBox.setOnClickListener(new OnClickListener() {

               @Override

               public void onClick(View v) {

                   if(isSelected.get(position)){

                      isSelected.put(position, false);

                   }else{

                      isSelected.put(position, true);

                   }

                   notifyDataSetChanged();

               }

            });

         

       

        return convertView;

    }

    }

   

    class ViewHolder{

       TextView title;

       TextView content;

       CheckBox checkBox;

    }

}

 

 

以下是main.xml文件的内容

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<ListView

    android:id="@+id/main_listview"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:layout_weight="0.1"

    ></ListView>

<LinearLayout android:layout_height="fill_parent"

    android:layout_width="fill_parent"

    android:layout_weight="0.9"

    android:orientation="horizontal">

    <Button android:id="@+id/main_all"

       android:layout_width="fill_parent"

       android:layout_height="fill_parent"

       android:layout_weight="0.5"

       android:text="全选选中"/>

    <Button android:id="@+id/main_none"

       android:layout_width="fill_parent"

       android:layout_height="fill_parent"

       android:layout_weight="0.5"

       android:text="全选取消"/>

    </LinearLayout>

 

</LinearLayout>

 

以下是listview子项item_listview.xml的内容

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<LinearLayout android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:layout_weight="0.7">

    <TextView android:id="@+id/item_listview_title"

       android:layout_width="fill_parent"

       android:layout_height="fill_parent"/>

    </LinearLayout>

<RelativeLayout android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:layout_weight="0.3">

    <CheckBox android:id="@+id/item_listview_checkbox"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_alignParentRight="true"

       android:layout_centerVertical="true"

       android:focusable="false"

       />

    <TextView

       android:id="@+id/item_listview_content"

       android:layout_width="wrap_content"

       android:layout_height="fill_parent"

       android:layout_alignParentLeft="true"

       android:layout_toLeftOf="@+id/item_listview_checkbox"

       android:layout_centerVertical="true"

       android:textSize="36dip"

       android:singleLine="true"

       android:ellipsize="marquee"

       android:marqueeRepeatLimit="marquee_forever"

       />

    </RelativeLayout>

   

</LinearLayout>