ExpandableListView 的简单用法

来源:互联网 发布:windows制作mac启动盘 编辑:程序博客网 时间:2024/04/28 03:57


直接附代码:

 package com.example.expandablelistviewtest;

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

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;
import android.widget.ExpandableListView.OnChildClickListener;

public class MainActivity extends Activity {

    private ExpandableListView myExpListview;
    private List<String> Kinds_grouplist;// 商品类父类
    private List<List<String>> Kinds_childlist;// 商品类子类

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData_Kindlist(null, null);
        myExpListview = (ExpandableListView) findViewById(R.id.myExpListview);
        myExpListview.setAdapter(new KindofGoodsAdapter(this, Kinds_grouplist,
                Kinds_childlist));
        myExpListview.setOnChildClickListener(new OnChildClickListener() {
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                String gpName = Kinds_grouplist.get(groupPosition);
                String clName = Kinds_childlist.get(groupPosition).get(
                        childPosition);
                Toast.makeText(MainActivity.this, gpName + " " + clName,
                        Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }

    /**
     * 初始化商品类
     */
    private void initData_Kindlist(List<String> grouplist,
            List<List<String>> childlist) {
        Kinds_grouplist = new ArrayList<String>();
        Kinds_childlist = new ArrayList<List<String>>();
        List list = new ArrayList<String>();
        for (int i = 0; i < 5; i++) {
            list.add("商品子类" + i);
        }
        addInfo("商品分类1", list);
        addInfo("商品分类2", list);
        addInfo("商品分类3", list);
    }

    /**
     * 添加一个商品类
     * @param g
     * @param c
     */
    private void addInfo(String group, List<String> child) {
        Kinds_grouplist.add(group);
        Kinds_childlist.add(child);
    }
}


package com.example.expandablelistviewtest;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class KindofGoodsAdapter extends BaseExpandableListAdapter{

    private Context context;
    private List<String> group;
    private List<List<String>> child;
    
    
    public KindofGoodsAdapter(Context context, List<String> group,
            List<List<String>> child) {
        super();
        this.context = context;
        this.group = group;
        this.child = child;
    }

    @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) {
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate
                    (R.layout.list_item, null);
            TextView Tv_kindName = (TextView) convertView.findViewById(R.id.textView);
            Tv_kindName.setText(group.get(groupPosition));
        }else {
            TextView Tv_kindName = (TextView) convertView.findViewById(R.id.textView);
            Tv_kindName.setText(group.get(groupPosition));
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate
                    (R.layout.list_item, null);
            TextView Tv_kindName = (TextView) convertView.findViewById(R.id.textView);
            Tv_kindName.setText(child.get(groupPosition).get(childPosition));
        }else {
            TextView Tv_kindName = (TextView) convertView.findViewById(R.id.textView);
            Tv_kindName.setText(child.get(groupPosition).get(childPosition));
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        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"
    tools:context="${relativePackage}.${activityClass}" >

    <ExpandableListView
        android:id="@+id/myExpListview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

</RelativeLayout>


list_item.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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="父条目"/>

</RelativeLayout>

child_list_item.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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:text="子条目"/>

</RelativeLayout>


6 0