码迷,mamicode.com
首页 > 其他好文 > 详细

expandablelistview 的用法

时间:2014-07-02 15:04:02      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   java   color   os   

package com.example.getshareperference;

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

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

public class MainActivity extends Activity {
    private ExpandableListView listview;
    private MyAdapter myAdapter;
    private List<String> group;
    private List<List<String>> child;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        listview = (ExpandableListView) findViewById(R.id.expandableListView1);
        myAdapter = new MyAdapter();
        listview.setAdapter(myAdapter);
        listview.setGroupIndicator(null);
    }

    public void initData() {
        group = new ArrayList<String>();
        child = new ArrayList<List<String>>();
        addInfo("湖北省", new String[] { "武汉市", "黄石市", "荆州市", "随州市", "宜昌市" });
        addInfo("湖南省", new String[] { "长沙市", "株洲市", "湘潭市", "衡阳市", "邵阳市" });
        addInfo("广东省", new String[] { "广州市", "深圳市", "珠海市", "汕头市", "佛山市" });
        addInfo("广西省", new String[] { "南宁市", "柳州市", "桂林市", "梧州市", "北海市" });
        
    }

    public void addInfo(String g, String[] c) {
        group.add(g);
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < c.length; i++) {
            list.add(c[i]);
        }
        child.add(list);
    }

    class MyAdapter extends BaseExpandableListAdapter {

        @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.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
            TextView textView = null;
            if (convertView == null) {
                textView = new TextView(MainActivity.this);
            }
            else{
                textView=(TextView)convertView;
            }
            textView.setText(group.get(groupPosition));
            textView.setTextSize(30);
            textView.setPadding(32,10, 10, 20);
            return textView;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            TextView textView = null;
            if (convertView == null) {
                textView = new TextView(MainActivity.this);
            }
            else{
                textView=(TextView)convertView;
            }
            textView.setText(child.get(groupPosition).get(childPosition));
            textView.setTextSize(20);
            textView.setPadding(60, 10, 0, 10);
            return textView;
        }

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

    }
}

 

expandablelistview 的用法,布布扣,bubuko.com

expandablelistview 的用法

标签:android   style   blog   java   color   os   

原文地址:http://www.cnblogs.com/mf0819/p/3819971.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!