BaseExpandableListAdapter的用法

MainActivity.java

package com.example.listviewdemo;

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

import android.app.Activity;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private List<String> groupdate;//定义组
    private List<List<String>> childrendate;//定义组中的子条目
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ExpandableListView expandable = (ExpandableListView)findViewById(R.id.expandableListView1);
        groupdate = new ArrayList<String>();
        groupdate.add("Java");
        groupdate.add("Android");
        groupdate.add("IOS");

        childrendate = new ArrayList<List<String>>();
        List<String> child1 = new ArrayList<String>();
        child1.add("java基础");
        child1.add("java大全");
        child1.add("解析java");

        childrendate.add(child1);
        List<String> child2 = new ArrayList<String>();
        child2.add("Android基础");
        child2.add("Android大全");
        child2.add("解析Android");
        childrendate.add(child2);

        List<String> child3 = new ArrayList<String>();
        child3.add("IOS基础");
        child3.add("IOS大全");
        child3.add("解析IOS");
        childrendate.add(child3);

        expandable.setAdapter(new ExpandableAdapter());
        /*expandable.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "你点击了"+groupPosition+","+ childPosition,Toast.LENGTH_SHORT).show();
                return false;
            }
        });*/
    }
class ExpandableAdapter extends BaseExpandableListAdapter {
        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return childrendate.get(groupPosition).get(childPosition);
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return 0;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
            TextView myText = null;
            if (convertView != null) {
                myText = (TextView) convertView;
                myText.setText(childrendate.get(groupPosition).get(
                        childPosition));
            } else {
                AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
                        ViewGroup.LayoutParams.FILL_PARENT, 50);
                myText = new TextView(MainActivity.this);
                myText.setLayoutParams(layoutParams);
                myText.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
                myText.setPadding(50, 0, 0, 0);
                myText.setText(childrendate.get(groupPosition).get(
                        childPosition));
            }
            return myText;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return childrendate.get(groupPosition).size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            return childrendate.get(groupPosition);
        }

        @Override
        public int getGroupCount() {
            return childrendate.size();
        }

        @Override
        public long getGroupId(int groupPosition) {
            return 0;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
            TextView myText = null;
            if (convertView != null) {
                myText = (TextView) convertView;
                myText.setText(groupdate.get(groupPosition));
            } else {
                myText = new TextView(MainActivity.this);
                AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
                        ViewGroup.LayoutParams.FILL_PARENT, 50);
                myText.setLayoutParams(layoutParams);
                myText.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
                myText.setPadding(50, 0, 0, 0);
                myText.setText(groupdate.get(groupPosition));
            }
            return myText;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }




}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.listviewdemo.MainActivity" >

    <ExpandableListView
        android:id="@+id/expandableListView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" >

    </ExpandableListView>

</RelativeLayout>

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值