关闭

BaseExpandableListAdapter的用法

标签: expandablelistview
146人阅读 评论(0) 收藏 举报
分类:

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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5452次
    • 积分:178
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:11篇
    • 译文:0篇
    • 评论:0条
    文章分类