首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android中ExpandableListView控件根本使用

2012-08-15 
Android中ExpandableListView控件基本使用本文采用一个Demo来展示Android中ExpandableListView控件的使用,

Android中ExpandableListView控件基本使用

本文采用一个Demo来展示Android中ExpandableListView控件的使用,如如何在组/子ListView中绑定数据源。先看下效果图再去代码:


Android中ExpandableListView控件根本使用
?
Android中ExpandableListView控件根本使用
?

?

<?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的显示方式(在另外一个布局文件里边)不用默认的方式          如果自定义listview的显示方式这里这个android:id="@id/android:list" 必须这样写 -->    <!-- android:drawSelectOnTop="false"此属性用来设置listview上的背景颜色会不会  挡住(覆盖)内容 , 如果这是为false就表示不会覆盖掉 -->       <ExpandableListView         android:id="@+id/m_list"                android:layout_width="fill_parent"                 android:layout_height="wrap_content"               android:layout_weight="1"                android:drawSelectorOnTop="false"/> </LinearLayout>

?

?

package com.andyidea.demo;import java.util.ArrayList;import java.util.List;import android.app.ExpandableListActivity;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.widget.AbsListView;import android.widget.BaseExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.TextView;import android.widget.ExpandableListView.OnGroupExpandListener;public class ContactsActivity extends ExpandableListActivity {List<String> group;           //组列表List<List<String>> child;     //子列表ContactsInfoAdapter adapter;  //数据适配器    int groupSize;    ExpandableListView mExpandableListView;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);  //设置为无标题        setContentView(R.layout.main);                initializeData();        getExpandableListView().setAdapter(new ContactsInfoAdapter());        getExpandableListView().setCacheColorHint(0);  //设置拖动列表的时候防止出现黑色背景                mExpandableListView = (ExpandableListView)findViewById(R.id.m_list);        //只展开一个group菜单        //mExpandableListView.setOnGroupExpandListener(new OnGroupExpandListener() {//public void onGroupExpand(int groupPosition) {//for (int i = 0; i < groupSize; i++) {//if (groupPosition != i) {//mExpandableListView.collapseGroup(i);//}//}//}//});    }        /**     * 初始化组、子列表数据     */    private void initializeData(){    group = new ArrayList<String>();    child = new ArrayList<List<String>>();        addInfo("Andy",new String[]{"male","138123***","GuangZhou"});    addInfo("Fairy",new String[]{"female","138123***","GuangZhou"});    addInfo("Jerry",new String[]{"male","138123***","ShenZhen"});    addInfo("Tom",new String[]{"female","138123***","ShangHai"});    addInfo("Bill",new String[]{"male","138231***","ZhanJiang"});        }        /**     * 模拟给组、子列表添加数据     * @param g-group     * @param c-child     */    private void addInfo(String g,String[] c){    group.add(g);    List<String> childitem = new ArrayList<String>();    for(int i=0;i<c.length;i++){    childitem.add(c[i]);    }    child.add(childitem);    }        class ContactsInfoAdapter extends BaseExpandableListAdapter{        //-----------------Child----------------//    @Overridepublic Object getChild(int groupPosition, int childPosition) {return child.get(groupPosition).get(childPosition);}        @Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}        @Overridepublic int getChildrenCount(int groupPosition) {return child.get(groupPosition).size();}        @Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {    String string = child.get(groupPosition).get(childPosition); return getGenericView(string);}        //----------------Group----------------//    @Overridepublic Object getGroup(int groupPosition) {return group.get(groupPosition);}@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}@Overridepublic int getGroupCount() {groupSize = group.size();return groupSize;}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {String string = group.get(groupPosition);  return getGenericView(string);}//创建组/子视图          public TextView getGenericView(String s) {              // Layout parameters for the ExpandableListView              AbsListView.LayoutParams lp = new AbsListView.LayoutParams(                      ViewGroup.LayoutParams.FILL_PARENT, 40);              TextView text = new TextView(ContactsActivity.this);              text.setLayoutParams(lp);              // Center the text vertically              text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);              // Set the text starting position              text.setPadding(36, 0, 0, 0);                            text.setText(s);              return text;          }  @Overridepublic boolean hasStableIds() {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn true;}        }}

?

?

?

附件就是一模一样的代码。这只是自己做了一下总结,只能提供参考一下。

热点排行