关闭

expandableListView checkbox

标签: androidlayoutstringnullencodingservice
4479人阅读 评论(0) 收藏 举报
分类:
 

package com.cdma;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;

public class ExListView extends Activity {
 private static final String G_TEXT = "G_TEXT";
 //private static final String G_CB = "G_CB";
 private static final String C_TITLE = "C_TITLE";
 private static final String C_TEXT = "C_TEXT";
 private static final String C_CB = "C_CB";
    
    List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();   
    List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
   
    List<Map<String, Boolean>> groupCheckBox = new ArrayList<Map<String,Boolean>>();
    List<List<Map<String, Boolean>>> childCheckBox = new ArrayList<List<Map<String,Boolean>>>();

 String CheckedItem="";
    ExpandBaseAdapter adapter;
    ExpandableListView exList;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //初始化数据Group Text,Child Title Text
        for (int i = 0; i < 3; i++) {
            Map<String, String> curGroupMap = new HashMap<String, String>();
            groupData.add(curGroupMap);
            curGroupMap.put(G_TEXT, "Group " + i);
             
            List<Map<String, String>> children = new ArrayList<Map<String, String>>();
            for (int j = 0; j < 3; j++) {
                Map<String, String> curChildMap = new HashMap<String, String>();
                children.add(curChildMap);
                curChildMap.put(C_TITLE, "Child " + j);
                curChildMap.put(C_TEXT, "Child " + j);
            }
            childData.add(children);
        }
      //初始化数据checkBox
        for ( int i = 0; i < 5; i++) {
         //Map<String, Boolean> curGB = new HashMap<String, Boolean> ();
            //groupCheckBox.add(curGB);
            //curGB.put(G_CB, false);
            List<Map<String, Boolean>> childCB = new ArrayList<Map<String,Boolean>>();
            for (int j = 0; j < 5; j++) {
                Map<String, Boolean> curCB = new HashMap<String, Boolean>();
                childCB.add(curCB);
                curCB.put(C_CB, false); //全不选
            }
            childCheckBox.add(childCB);
        }
               
        adapter = new ExpandBaseAdapter(ExListView.this,
          groupData, childData, groupCheckBox, childCheckBox);
        exList  = (ExpandableListView) findViewById(R.id.layoutExListView);
  exList.setAdapter(adapter);
  
  // 去掉系统自带的按钮
  exList.setGroupIndicator(null);
  // 去掉系统自带的分隔线
  exList.setDivider(null);
  // 展开所有二级列表
  int groupCount = adapter.getGroupCount();
  for (int i = 0; i < groupCount; i++) {
   exList.expandGroup(i);
  }
  // 监听二级列表
  exList.setOnChildClickListener(new OnChildClickListener() {
   public boolean onChildClick(ExpandableListView parent, View v,
     int groupPosition, int childPosition, long id) {
    
    CheckBox checkBox = (CheckBox) v.findViewById(R.id.multiple_checkbox);     
    checkBox.toggle();

    if (childCheckBox.get(groupPosition).get(childPosition).get(C_CB)) {
     childCheckBox.get(groupPosition).get(childPosition).put(C_CB, false);     
    }
    else {
     childCheckBox.get(groupPosition).get(childPosition).put(C_CB, true);
    }
    //checkBox.setChecked(childCheckBox.get(groupPosition).get(childPosition).get(C_CB));
    CheckedItem = "";
    for (int i=0; i < adapter.getGroupCount(); i++) {
     for (int j=0; j < adapter.getChildrenCount(i); j++) {
      if (childCheckBox.get(i).get(j).get(C_CB)) {
       CheckedItem = CheckedItem
        + groupData.get(i).get(G_TEXT) + ":"
        + childData.get(i).get(j).get(C_TITLE)+" ";
      }
     }
     
    }
    Toast.makeText(ExListView.this, CheckedItem, Toast.LENGTH_LONG).show();
    /*
    Toast.makeText(getBaseContext(),String.valueOf(groupPosition) + ":" + String.valueOf(childPosition),
      Toast.LENGTH_SHORT).show();
    */
    return false;
   }
  });
    }
}

/* static class ViewHolder {
  TextView cTitle;
  TextView cText;
  CheckBox checkBox;
 }
    class ExAdapter extends BaseExpandableListAdapter {
     ExListView exlistview;
     ViewHolder holder;
     
     public ExAdapter(ExListView elv) {
      super();
      exlistview = elv;
     }
     public View getGroupView(int groupPosition, boolean isExpanded,
       View convertView, ViewGroup parent) {
   View view = convertView;
   if (view == null) {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.groupitem, null);
   }
   TextView title = (TextView) view.findViewById(R.id.content_001);
   title.setText(getGroup(groupPosition).toString());
   ImageView image=(ImageView) view.findViewById(R.id.groupBox);
   if(isExpanded)
    image.setBackgroundResource(R.drawable.groupdown);
   else image.setBackgroundResource(R.drawable.groupleft);
    
      return view;
     }

     public long getGroupId(int groupPosition) {
      return groupPosition;
     }
     
     public Object getGroup(int groupPosition) {
      return groupData.get(groupPosition).get(G_TEXT).toString();
     }

     public int getGroupCount() {
   return groupData.size();

     }
     //**************************************
     public View getChildView(int groupPosition, int childPosition,
       boolean isLastChild, View convertView, ViewGroup parent) {
      //View view = convertView;
   if (convertView == null) {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.childitem, null); 
    holder = new ViewHolder();
    holder.cTitle = (TextView) convertView.findViewById(R.id.child_title);
    holder.cText = (TextView) convertView.findViewById(R.id.child_text);
    holder.checkBox = (CheckBox) convertView.findViewById(R.id.multiple_checkbox);
    convertView.setTag(holder);
   } else {
    holder = (ViewHolder)convertView.getTag();
   }
   holder.cTitle.setText(childData.get(groupPosition).get(childPosition).get(C_TITLE).toString());
   holder.cText.setText(childData.get(groupPosition).get(childPosition).get(C_TEXT).toString());
   holder.checkBox.setChecked(childCheckBox.get(groupPosition).get(childPosition).get(C_CB));
   return convertView;
     }

     public long getChildId(int groupPosition, int childPosition) {
      return childPosition;
     }
     
     public Object getChild(int groupPosition, int childPosition) {
      return childData.get(groupPosition).get(childPosition).get(C_TITLE).toString();
     }

     public int getChildrenCount(int groupPosition) {
      return childData.get(groupPosition).size();
     }
     //**************************************
     public boolean hasStableIds() {
      return true;
     }

     public boolean isChildSelectable(int groupPosition, int childPosition) {
      return true;
     }

    }*/

 

 

package com.cdma;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class ExpandBaseAdapter extends BaseExpandableListAdapter {
 
 private static final String G_TEXT = "G_TEXT";
 //private static final String G_CB = "G_CB";
 private static final String C_TITLE = "C_TITLE";
 private static final String C_TEXT = "C_TEXT";
 private static final String C_CB = "C_CB";
    
    List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();   
    List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
   
    List<Map<String, Boolean>> groupCheckBox = new ArrayList<Map<String,Boolean>>();
    List<List<Map<String, Boolean>>> childCheckBox = new ArrayList<List<Map<String,Boolean>>>();
   
 private Context context;
 //ExListView exlistview;
 ViewHolder holder;

 public ExpandBaseAdapter(Context context,
   List<Map<String, String>> groupData, List<List<Map<String, String>>> childData,
   List<Map<String, Boolean>> groupCheckBox, List<List<Map<String, Boolean>>> childCheckBox) {
  this.groupData = groupData;
  this.childData = childData;
  this.groupCheckBox = groupCheckBox;
  this.childCheckBox = childCheckBox;
  this.context = context;
 }

 public View getGroupView(int groupPosition, boolean isExpanded,
   View convertView, ViewGroup parent) {
  View view = convertView;
  if (view == null) {
   LayoutInflater inflater = (LayoutInflater)
    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   view = inflater.inflate(R.layout.groupitem, null);
  }
  TextView title = (TextView) view.findViewById(R.id.groupText);
  title.setText(getGroup(groupPosition).toString());
  ImageView image = (ImageView) view.findViewById(R.id.groupBox);
  if (isExpanded)
   image.setBackgroundResource(R.drawable.groupdown);
  else
   image.setBackgroundResource(R.drawable.groupleft);

  return view;
 }

 public long getGroupId(int groupPosition) {
  return groupPosition;
 }

 public Object getGroup(int groupPosition) {
  return groupData.get(groupPosition).get(G_TEXT).toString();
 }

 public int getGroupCount() {
  return groupData.size();

 }

 // **************************************
 public View getChildView(int groupPosition, int childPosition,
   boolean isLastChild, View convertView, ViewGroup parent) {
  // View view = convertView;
  if (convertView == null) {
   LayoutInflater inflater = (LayoutInflater)
    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   convertView = inflater.inflate(R.layout.childitem, null);
   holder = new ViewHolder();
   holder.cTitle = (TextView) convertView
     .findViewById(R.id.child_title);
   holder.cText = (TextView) convertView.findViewById(R.id.child_text);
   holder.checkBox = (CheckBox) convertView
     .findViewById(R.id.multiple_checkbox);
   convertView.setTag(holder);
  } else {
   holder = (ViewHolder) convertView.getTag();
  }
  holder.cTitle.setText(childData.get(groupPosition).get(childPosition)
    .get(C_TITLE).toString());
  holder.cText.setText(childData.get(groupPosition).get(childPosition)
    .get(C_TEXT).toString());
  holder.checkBox.setChecked(childCheckBox.get(groupPosition)
    .get(childPosition).get(C_CB));
  return convertView;
 }

 public long getChildId(int groupPosition, int childPosition) {
  return childPosition;
 }

 public Object getChild(int groupPosition, int childPosition) {
  return childData.get(groupPosition).get(childPosition).get(C_TITLE)
    .toString();
 }

 public int getChildrenCount(int groupPosition) {
  return childData.get(groupPosition).size();
 }

 // **************************************
 public boolean hasStableIds() {
  return true;
 }

 public boolean isChildSelectable(int groupPosition, int childPosition) {
  return true;
 }

 static class ViewHolder {
  TextView cTitle;
  TextView cText;
  CheckBox checkBox;
 }
}

 

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <!--  android:background="@drawable/default_bg"> -->
 <ExpandableListView
  android:id="@+id/layoutExListView"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"/>
</RelativeLayout>

<?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="40dip"
 android:layout_gravity="center_horizontal">
 <LinearLayout
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/layout_01">
  <ImageView
   android:id="@+id/groupImaView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
           
            android:src="@drawable/group_icon"/> 
  <RelativeLayout
   android:id="@+id/layout_02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
   <TextView android:id="@+id/groupText"
          android:textSize="26px"
       android:layout_width="wrap_content"
       android:layout_height="fill_parent"
       android:textColor="#FFFFFF"
       android:gravity="center_vertical"
       android:paddingLeft="10px"
       android:layout_gravity="center_vertical"/>
   <ImageView
    android:id="@+id/groupBox"
    android:layout_width="wrap_content"
                android:layout_height="wrap_content"                
                android:layout_alignParentRight="true"/>
  </RelativeLayout>      
 </LinearLayout>
</LinearLayout>

 

<?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:id="@+id/childlayout"
 android:orientation="horizontal">
    <RelativeLayout
     android:layout_width="fill_parent"
     android:id="@+id/relativeLayout1"
     android:layout_height="wrap_content">   
     <ImageView android:id="@+id/childImageView"
         android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/child_icon"
            android:paddingTop="10dip"
            android:layout_marginLeft="30dip"/>
   <LinearLayout
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:orientation="vertical"
     android:layout_toRightOf="@id/childImageView">
       <TextView
             android:id="@+id/child_title"
             android:layout_width="wrap_content"
    android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:textSize="20dip"
                android:layout_gravity="center_vertical"/>
       <TextView
             android:id="@+id/child_text"
             android:layout_width="wrap_content"
    android:layout_height="wrap_content"
             android:textSize="15dip"
              android:gravity="center_vertical"
              android:layout_gravity="center_vertical"/>
   </LinearLayout>
     <RatingBar
      android:id="@+id/ratingbar"
      android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_marginTop="15dp"
   android:numStars="5"
   style="?android:attr/ratingBarStyleSmall"
   android:gravity="center_vertical"
   android:layout_toLeftOf="@+id/multiple_checkbox"/>
  <!--这三个很重要
   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:focusable="false"
    android:focusableInTouchMode="false"
    android:clickable="false"
    android:layout_alignParentRight="true" android:checked="true"/>
 </RelativeLayout>
</LinearLayout>

0
0

猜你在找
【视频】C语言及程序设计(讲师:贺利坚)
【视频】Python爬虫工程师培养课程全套(讲师:韦玮)
【视频】Python全栈开发入门与实战(讲师:李杰)
【视频】2017软考网络规划设计师套餐(讲师:任铄)
【视频】2017软考软件设计师套餐(讲师:任铄)
【视频】2017软考信息系统项目管理师套餐(讲师:任铄)
【视频】软考(高级)项目经理实战营(讲师:张传波)
【视频】微信公众平台开发套餐(讲师:刘运强)
深度学习原理+实战+算法+主流框架套餐(讲师:唐宇迪)
2017系统集成项目管理工程师通关套餐(讲师:徐朋)
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:318771次
    • 积分:2749
    • 等级:
    • 排名:第12647名
    • 原创:41篇
    • 转载:59篇
    • 译文:0篇
    • 评论:41条
    文章分类
    最新评论