android ExpandableListActivity 练习

不足之处请多多指正,共同学习


public class MainActivity extends ExpandableListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ExpandableListAdapter adapter = new ExpandableListAdapter() {
            String[] groups = new String[]
                    { "神族兵种", "虫族兵种", "人族兵种"};
            String[][] child = new String[][]
                    {
                            { "狂战士", "龙骑士", "黑暗圣堂", "电兵" },
                            { "小狗", "刺蛇", "飞龙", "自爆飞机" },
                            { "机枪兵", "护士MM" , "幽灵" }
                    };
            @Override
            public void registerDataSetObserver(DataSetObserver observer) {

            }

            @Override
            public void unregisterDataSetObserver(DataSetObserver observer) {

            }

            @Override
            public int getGroupCount() {
                return groups.length;
            }

            @Override
            public int getChildrenCount(int groupPosition) {
                return child[groupPosition].length;
            }

            @Override
            public Object getGroup(int groupPosition) {
                return groups[groupPosition];
            }

            @Override
            public Object getChild(int groupPosition, int childPosition) {
                return child[groupPosition][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) {
                TextView v;
                if (convertView == null){
                    v = new TextView(MainActivity.this);
                    v.setPadding(45,0,0,0);
                    v.setTextSize(20);
                }else{
                    v = (TextView) convertView;
                }
                v.setText(groups[groupPosition]);
                return v;
            }
            @Override
            public View getChildView(int groupPosition, int childPosition,
                                     boolean isLastChild, View convertView, ViewGroup parent) {
                TextView v;
                if (convertView == null){
                    v = new TextView(MainActivity.this);
                    v.setPadding(60,0,0,0);
                    v.setTextSize(15);
                }else{
                    v = (TextView) convertView;
                }
                v.setText(child[groupPosition][childPosition]);
                return v;
            }
            @Override
            public boolean isChildSelectable(int groupPosition, int childPosition) {
                return true;
            }
            @Override
            public boolean areAllItemsEnabled() {
                return true;
            }
            @Override
            public boolean isEmpty() {
                return false;
            }
            @Override
            public void onGroupExpanded(int groupPosition) {
                //选项组展开
            }
            @Override
            public void onGroupCollapsed(int groupPosition) {
                //选项组折叠
            }

            @Override
            public long getCombinedChildId(long groupId, long childId) {
                return childId;
            }

            @Override
            public long getCombinedGroupId(long groupId) {
                return groupId;
            }
        };
        setListAdapter(adapter);
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bdawn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值