android expandablelistview



若是要看原文和详细,请支持别人的原创。

请连接转载。

转载地址:

http://www.cnblogs.com/sczjhh/archive/2012/11/26/szh.html





package com.eyu.activity_test;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * @author Administrator
 * @date &{date}
 */
public class ExpandableAdapter extends BaseExpandableListAdapter{
    private Context context;
    public ExpandableAdapter(Context context){
        this.context = context;
    }
    
    int[] logos = new int[] { R.drawable.wei, R.drawable.shu,R.drawable.wu};
    //设置组视图的显示文字
    private String[] generalsTypes = new String[] { "魏", "蜀", "吴" };
    //子视图显示文字
    private String[][] generals = new String[][] {
            { "夏侯惇", "甄姬", "许褚", "郭嘉", "司马懿", "杨修" },
            { "马超", "张飞", "刘备", "诸葛亮", "黄月英", "赵云" },
            { "吕蒙", "陆逊", "孙权", "周瑜", "孙尚香" }

    };
    //子视图图片
    public int[][] generallogos = new int[][] {
            { R.drawable.xiahoudun, R.drawable.zhenji,
                    R.drawable.xuchu, R.drawable.guojia,
                    R.drawable.simayi, R.drawable.yangxiu },
            { R.drawable.machao, R.drawable.zhangfei,
                    R.drawable.liubei, R.drawable.zhugeliang,
                    R.drawable.huangyueying, R.drawable.zhaoyun },
            { R.drawable.lvmeng, R.drawable.luxun, R.drawable.sunquan,
                    R.drawable.zhouyu, R.drawable.sunshangxiang } };
    
    //自己定义一个获得文字信息的方法
    TextView getTextView() {
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, 84);
        TextView textView = new TextView(
                context);
        textView.setLayoutParams(lp);
        textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
        textView.setPadding(36, 0, 200, 0);
        textView.setTextSize(20);
        textView.setTextColor(Color.BLACK);
        return textView;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return generals[groupPosition][childPosition];
    }

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

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LinearLayout ll = new LinearLayout(
                context);
        ll.setOrientation(0);
        ImageView generallogo = new ImageView(
                context);
        generallogo
                .setImageResource(generallogos[groupPosition][childPosition]);
        ll.addView(generallogo);
        TextView textView = getTextView();
        textView.setText(getChild(groupPosition, childPosition)
                .toString());
        ll.addView(textView);
        return ll;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return generals[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return generalsTypes[groupPosition];
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return generalsTypes.length;
    }

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LinearLayout ll = new LinearLayout(
                context);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        ImageView logo = new ImageView(context);
        logo.setImageResource(logos[groupPosition]);
        logo.setPadding(0, 0, 0, 0);
        ll.addView(logo);
        TextView textView = getTextView();
        textView.setTextColor(Color.BLACK);
        textView.setText(getGroup(groupPosition).toString());
        ll.addView(textView);
          
        ImageView iv  =new ImageView(context);
        iv.setPadding(70, 0, 0, 0);
        if (isExpanded) {
            iv.setBackgroundResource(R.drawable.btn1);
        } else {
            iv.setBackgroundResource(R.drawable.btn2);
        }
        ll.addView(iv);

        return ll;
//        LinearLayout ll = (LinearLayout) View.inflate(    context, R.layout.list_item    , null);
//        TextView tv = (TextView)ll.findViewById(R.id.tv);
//        ImageView iv1 = (ImageView)ll.findViewById(R.id.iv1);
//        ImageView iv2 = (ImageView)ll.findViewById(R.id.iv2);
//        tv.setText(getGroup(groupPosition).toString());
//        iv1.setBackgroundResource(logos[groupPosition]);
//        if (isExpanded) {
//            
//            iv2.setBackgroundResource(R.drawable.btn1);
//        } else {
//            iv2.setBackgroundResource(R.drawable.btn2);
//        }
//        return null;
    }

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

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

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值