ExpandableListView 小例子

package com.czy.android.demo;

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

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class ExpandableListDemo extends Activity {
    /** Called when the activity is first created. */
    List<String> group;
    List<List<String>> child;
    ExpandableListView expandableList;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        expandableList = (ExpandableListView) this.findViewById(R.id.expand_list);
        initialData();
        InfoDetailsAdapter adapter = new InfoDetailsAdapter(this);
        expandableList.setAdapter(adapter);
       
    }
    public void addInfo(String p,String[] c){
        group.add(p);
        List<String> item = new ArrayList<String>();
        for(int i= 0;i<c.length;i++){
            item.add(c[i]);
        }
        child.add(item);
    }
   
    public void initialData(){
        group = new ArrayList<String>();
        child = new ArrayList<List<String>>();
        addInfo("我的好友", new String[]{"田华","志萍","莉萍"});
        addInfo("小学同学",new String[]{"易琪坤","王营盈","阮昭"});
        addInfo("大学同学",new String[]{"洪涛","唐哲伟"});
    }
   
    public class InfoDetailsAdapter extends BaseExpandableListAdapter{

        Activity context;
        public InfoDetailsAdapter(Activity context){
            this.context = context;
        }
       
        @Override
        public int getGroupCount() {
            // TODO Auto-generated method stub
            return group.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            // TODO Auto-generated method stub
            return child.get(groupPosition).size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            // TODO Auto-generated method stub
            return group.get(groupPosition);
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return child.get(groupPosition).get(childPosition);
        }

        @Override
        public long getGroupId(int groupPosition) {
            // TODO Auto-generated method stub
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            String str = group.get(groupPosition).toString();
            return getGenericView(str);
        }

        @Override
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            String str = child.get(groupPosition).get(childPosition).toString();
            return getGenericView(str);
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return false;
        }
       
        public TextView getGenericView(String s){
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 64);
            TextView text = new TextView(context);
            text.setLayoutParams(lp);
            text.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
            text.setPadding(50, 0, 0, 0);
            text.setText(s);
            return text;
        }
       
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值