Android开发 BaseExpandableListAdapter的使用

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

项目需要展示一个通讯簿,通讯簿中的手机号码是分组的,要求勾选组时,自动勾选组下的手机号码,实现效果如下:

  

下面是实现步骤。

1、新建类PhoneListItem,用于表示分组中的每一个手机号码。

 

package com.ydtf.android;

 

public class PhoneListItem {

 public String phone,name;

 public boolean checked;

 public PhoneListItem(String _name,String _phone,boolean _checked){

  name=_name;

  phone=_phone;

  checked=_checked;

 }

}

2、新建布局文件phone_list_item.xml,用于定义分组下面的每一条手机记录的页面布局。

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

    android:layout_width="fill_parent" android:layout_height="wrap_content"  

    android:orientation="horizontal" android:minHeight="40px"  

    android:layout_gravity="center_vertical">  

    <CheckBox android:id="@+id/phone_check" android:focusable="false"  

        android:layout_width="wrap_content" android:layout_height="wrap_content"  

        android:layout_marginLeft="35px" android:checked="false"/>  

    <TextView android:id="@+id/phone_name" android:layout_width="80dip"  

        android:layout_height="wrap_content" android:layout_gravity="center_vertical" />  

 

    <TextView android:id="@+id/phone_number" android:layout_width="wrap_content"  

        android:layout_height="wrap_content" android:textColor="?android:attr/textColorPrimary"  

        android:paddingLeft="10px" android:layout_gravity="center_vertical" />  

</LinearLayout>

3、新建类PhoneGroup,用于表示一个分组。

 

 

import java.util.List;

 

public class PhoneGroup {

 public String title;

 private boolean checked;

 public  List<PhoneListItem> children;  

 public PhoneGroup(String title,boolean checked,List<PhoneListItem> children){

  this.title=title;

  setChecked(checked);

  this.children=children;

 }

 public boolean getChecked(){

  return checked;

 }

 public void setChecked(boolean b){

  checked=b;

  if(children!=null&&children.size()>0){//若children不为空,循环设置children的checked

   for(PhoneListItem each : children){

    each.checked=checked;

   }

  }

 }

}

4、新建Activity布局文件phone_browser.xml,用于定义通讯簿的展现页面。

 

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

    android:layout_width="fill_parent" android:layout_height="fill_parent"  

    android:orientation="vertical">  

    <ExpandableListView android:id="@+id/phonelist"  

        android:layout_width="fill_parent" android:layout_height="0dip"  

        android:layout_weight="1" />    

</LinearLayout>

 

 

5、新建类QxtPhoneSelect,用于呈现通讯簿的Activity页面。

 

package com.ydtf.android;

 

import java.util.ArrayList;

import java.util.List;

 

import com.ydtf.android.PhoneGroupAdapter.ExpandableListHolder;

 

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.ExpandableListView;

public class QxtSelectPhone extends Activity implements 

ExpandableListView.OnGroupClickListener,ExpandableListView.OnChildClickListener{

 private List<PhoneGroup> groups;  

   

    private PhoneGroupAdapter exlist_adapter = null;  

    private ExpandableListView exlist;  

 

 public void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        //加载layout

        setContentView(R.layout.phone_browser);  

        //取得listview

        exlist = (ExpandableListView) findViewById(R.id.phonelist); 

       //调用init方法,这个方法主要是,初始化一些数据

        init();

        //构建expandablelistview的适配器

        exlist_adapter = new PhoneGroupAdapter(this, groups);  

        exlist.setOnChildClickListener(this);

        exlist.setAdapter(exlist_adapter);  //绑定视图-适配器

 

 }

    private void init() {  

     groups = new ArrayList<PhoneGroup>();

       

        //构建List用作group1的子项

        List<PhoneListItem> group1_children = new ArrayList<PhoneListItem>();

        //往List中添加内容

        PhoneListItem item = new PhoneListItem("和文明","1308763994", false);  

        group1_children.add(item);  

        item = new PhoneListItem("黄文明","1308763994", false);  

        group1_children.add(item);  

        item = new PhoneListItem("王文明","1308763994", false);  

        group1_children.add(item);  

 

        //拼装成 PhoneGroup

        PhoneGroup phonegroup1=new PhoneGroup("group1",false,group1_children);

 

        //------把前面的代码复制一遍,再添加一个组group2

        //构建List用作group2的子项

        List<PhoneListItem> group2_children = new ArrayList<PhoneListItem>();

        //往List中添加内容

        item = new PhoneListItem("张文明","1589065423", false);  

        group2_children.add(item);  

        item = new PhoneListItem("李文明","1589065423", false);  

        group2_children.add(item);  

        item = new PhoneListItem("赵文明","1589065423", false);  

        group2_children.add(item);  

 

        //拼装成 PhoneGroup

        PhoneGroup phonegroup2=new PhoneGroup("group2",false,group2_children);

        //添加进groups数组

        groups.add(phonegroup1);

        groups.add(phonegroup2);

    }

    //当分组行背点击时,让分组呈现“选中/取消选中”状态。

 @Override

 public boolean onChildClick(ExpandableListView parent, View v,

   int groupPosition, int childPosition, long id) {

  PhoneGroupAdapter.ExpandableListHolder holder=(ExpandableListHolder) v.getTag(); 

  holder.chkChecked.setChecked(!holder.chkChecked.isChecked());

  groups.get(groupPosition).children.get(childPosition).checked=

   !groups.get(groupPosition).children.get(childPosition).checked;

  return false;

 }

 @Override

 public boolean onGroupClick(ExpandableListView parent, View v,

   int groupPosition, long id) {

//  groups.get(groupPosition).setChecked(!groups.get(groupPosition).getChecked());

//  exlist_adapter.notifyDataSetChanged();

//  

  return false;

 }  

}

6、新建类PhoneGroupAdapter,实现BaseExpandableListAdapter。
package com.ydtf.android;
import java.util.List;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
public class PhoneGroupAdapter extends BaseExpandableListAdapter {
 class ExpandableListHolder {  //定义一个内部类,用于保存listitem的3个子视图引用,2个textview和1个checkbox
        TextView tvName;  
        TextView tvPhone;  
        CheckBox chkChecked;  
    } 
 private Context context;       //父activity
 private LayoutInflater mChildInflater;  //用于加载listitem的布局xml
 private LayoutInflater mGroupInflater;  //用于加载group的布局xml
 private List<PhoneGroup> groups;     //所有group
 //构造方法:参数c - activity,参数group - 所有group
 public PhoneGroupAdapter(Context c,List<PhoneGroup> groups){
  context=c;
  mChildInflater = (LayoutInflater) context
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  mGroupInflater = (LayoutInflater) context
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  this.groups = groups;
 }
 @Override
 public Object getChild(int arg0, int arg1) {//根据组索引和item索引,取得listitem   // TODO Auto-generated method stub
  return groups.get(arg0).children.get(arg1);
 }
 @Override
 public long getChildId(int arg0, int arg1) {//返回item索引
  return arg1;
 }
 @Override
 public int getChildrenCount(int groupPosition) {//根据组索引返回分组的子item数
  return groups.get(groupPosition).children.size();
 }
 @Override
 public Object getGroup(int groupPosition) {//根据组索引返回组
  return groups.get(groupPosition);
 }
 @Override
 public int getGroupCount() {//返回分组数
  return groups.size();
 }
 @Override
 public long getGroupId(int groupPosition) {//返回分组索引
  return groupPosition;
 }
 @Override
 public View getGroupView(int position, boolean isExpanded,
   View view, ViewGroup parent) {//根据组索引渲染"组视图"
ExpandableListHolder holder = null;  //清空临时变量holder
        if (view == null) {  //判断view(即view是否已构建好)是否为空
          
          //若组视图为空,构建组视图。注意flate的使用,R.layout.browser_expandable_list_item代表了
          //已加载到内存的browser_expandable_list_item.xml文件
            view = mGroupInflater.inflate(  
                    R.layout.phone_list_item, null);  
            //下面主要是取得组的各子视图,设置子视图的属性。用tag来保存各子视图的引用
            holder = new ExpandableListHolder();  
            //从view中取得textView
            holder.tvName = (TextView) view.findViewById(R.id.phone_name);  
            //从view中取得textview
            holder.tvPhone = (TextView) view.findViewById(R.id.phone_number);  
            //从view中取得checkbox
            holder.chkChecked = (CheckBox) view  
                    .findViewById(R.id.phone_check);      
//            holder.chkChecked.setEnabled(false);//禁用checkbox
            //把checkbox、textview的引用保存到组视图的tag属性中
            view.setTag(holder);   
        } else {  //若view不为空,直接从view的tag属性中获得各子视图的引用
            holder = (ExpandableListHolder) view.getTag();  
        }
        //对应于组索引的组数据(模型)
  PhoneGroup info = this.groups.get(position);  
       if (info != null) {  
          //根据模型值设置textview的文本
            holder.tvName.setText(info.title);  
            //根据模型值设置checkbox的checked属性
            holder.chkChecked.setChecked(info.getChecked()); 
       holder.chkChecked.setTag(info);
            holder.chkChecked.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
         PhoneGroup group=(PhoneGroup)v.getTag();
         group.setChecked(!group.getChecked());
         notifyDataSetChanged();
        }
            });
        }  
  // TODO Auto-generated method stub
  return view;
 }
    //行渲染方法  
 @Override
    public View getChildView(int groupPosition, int childPosition,  
            boolean isLastChild, View convertView, ViewGroup parent) {  
        ExpandableListHolder holder = null;  //清空临时变量
        if (convertView == null) {  //若行未初始化
          //通过flater初始化行视图
            convertView = mChildInflater.inflate(  
                    R.layout.phone_list_item, null);  
            //并将行视图的3个子视图引用放到tag中
            holder = new ExpandableListHolder();  
            holder.tvName = (TextView) convertView  
                    .findViewById(R.id.phone_name);  
              
            holder.tvPhone = (TextView) convertView.findViewById(R.id.phone_number);  
            holder.chkChecked = (CheckBox) convertView  
                    .findViewById(R.id.phone_check);
//            holder.chkChecked.setEnabled(false);
            convertView.setTag(holder);
        } else {  //若行已初始化,直接从tag属性获得子视图的引用
            holder = (ExpandableListHolder) convertView.getTag();  
        }  
        //获得行数据(模型)
        PhoneListItem info = this.groups.get(groupPosition)  
                .children.get(childPosition);  
        
        if (info != null) {  
          //根据模型数据,设置行视图的控件值
            holder.tvName.setText(info.name);  
            holder.tvPhone.setText(info.phone);  
            holder.chkChecked.setChecked(info.checked); 
            holder.chkChecked.setTag(info);
            holder.chkChecked.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
     CheckBox check=(CheckBox)v;
     PhoneListItem item=(PhoneListItem)v.getTag();
     item.checked=!item.checked;
//      check.setChecked(!check.isChecked());
    }
              
            });
        }  
        return convertView;  
    } 
 @Override
 public boolean hasStableIds() {//行是否具有唯一id
  return false;
 }
 @Override
 public boolean isChildSelectable(int groupPosition, int childPosition) {//行是否可选   
  return true;
 }
}

 

 


           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您想了解安卓开发 ExpandableListView 的使用示例,以下是一个简单的示例: 1. 定义适配器类 ``` public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context mContext; private List<String> mGroupTitles; // 父分组数据 private HashMap<String, List<String>> mChildData; // 子分组数据 public ExpandableListAdapter(Context context, List<String> groupTitles, HashMap<String, List<String>> childData) { mContext = context; mGroupTitles = groupTitles; mChildData = childData; } @Override public int getGroupCount() { return mGroupTitles.size(); } @Override public int getChildrenCount(int groupPosition) { return mChildData.get(mGroupTitles.get(groupPosition)).size(); } @Override public Object getGroup(int groupPosition) { return mGroupTitles.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return mChildData.get(mGroupTitles.get(groupPosition)).get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return true; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.list_item_group, null); TextView title = (TextView) view.findViewById(R.id.group_title); title.setText(mGroupTitles.get(groupPosition)); return view; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.list_item_child, null); TextView title = (TextView) view.findViewById(R.id.child_title); title.setText(mChildData.get(mGroupTitles.get(groupPosition)).get(childPosition)); return view; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } ``` 2. 定义布局文件 list_item_group.xml ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingLeft="40dp" android:paddingRight="40dp" android:paddingTop="20dp" android:paddingBottom="20dp"> <TextView android:id="@+id/group_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" /> </LinearLayout> ``` list_item_child.xml ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingLeft="40dp" android:paddingRight="40dp" android:paddingTop="15dp" android:paddingBottom="15dp"> <TextView android:id="@+id/child_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" /> </LinearLayout> ``` 3. 在 Activity 中使用 ExpandableListView ``` public class MainActivity extends AppCompatActivity { private ExpandableListView mExpandableListView; private ExpandableListAdapter mAdapter; private List<String> mGroupTitles; private HashMap<String, List<String>> mChildData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化数据 initData(); // 初始化 ExpandableListView mExpandableListView = (ExpandableListView) findViewById(R.id.expandable_list_view); mAdapter = new ExpandableListAdapter(this, mGroupTitles, mChildData); mExpandableListView.setAdapter(mAdapter); // 设置子项点击监听器 mExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(MainActivity.this, "您点击了" + mChildData.get(mGroupTitles.get(groupPosition)).get(childPosition), Toast.LENGTH_SHORT).show(); return true; } }); } private void initData() { mGroupTitles = new ArrayList<>(); mGroupTitles.add("计算机"); mGroupTitles.add("自然科学"); mGroupTitles.add("哲学"); List<String> computers = new ArrayList<>(); computers.add("Java"); computers.add("Python"); computers.add("C++"); computers.add("PHP"); computers.add("JavaScript"); computers.add("HTML/CSS"); computers.add("SQL"); List<String> sciences = new ArrayList<>(); sciences.add("物理"); sciences.add("化学"); sciences.add("生物"); sciences.add("地理"); sciences.add("天文"); List<String> philosophies = new ArrayList<>(); philosophies.add("伦理学"); philosophies.add("形而上学"); philosophies.add("认识论"); philosophies.add("逻辑学"); philosophies.add("美学"); mChildData = new HashMap<>(); mChildData.put(mGroupTitles.get(0), computers); mChildData.put(mGroupTitles.get(1), sciences); mChildData.put(mGroupTitles.get(2), philosophies); } } ``` 这样,您就可以展示一个带父分组和子分组的 ExpandableListView ,同时设置子项点击监听器,实现相应的功能了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值