多级列表的实现

215452_IvUc_2282721.jpg

package com.example.expandable;

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.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.Toast;

public class MainActivity extends Activity {
    private ExpandableListView expandableListView;
    private List<String> groupList;
    private List<List<String>> childList;
    private MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
        groupList = new ArrayList<String>();
        childList = new ArrayList<List<String>>();
        initDatas();
        adapter = new MyAdapter(this, groupList, childList);
        expandableListView.setAdapter(adapter);
        expandableListView.setGroupIndicator(getResources().getDrawable(R.drawable.expand_select));
        expandableListView.setOnGroupClickListener(new OnGroupClickListener() {
            
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                    int groupPosition, long id) {
                Toast.makeText(getApplicationContext(), groupList.get(groupPosition), 
                        Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        expandableListView.setOnChildClickListener(new OnChildClickListener() {
            
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                Toast.makeText(getApplicationContext(), childList.get(groupPosition).get(childPosition), 
                        Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }

    private void initDatas() {
        updateDatas("感兴趣男明星", new String[] { "刘德华", "张学友", "郭富城" });
        updateDatas("感兴趣女明星", new String[] { "刘亦菲", "张馨予", "郭采洁" });
    }

    private void updateDatas(String groupItem, String[] childs) {
        groupList.add(groupItem);
        List<String> childsItem = new ArrayList<String>();
        for (String item : childs) {
            childsItem.add(item);
        }
        childList.add(childsItem);
    }
}
package com.example.expandable;

import java.util.List;

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

public class MyAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<String> groupList;
    private List<List<String>> childList;
    private LayoutInflater layoutInflater;
    
    public MyAdapter(Context context, List<String> groupList,
            List<List<String>> childList) {
        this.context = context;
        this.groupList = groupList;
        this.childList = childList;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return groupList.size();
    }

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

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return groupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childList.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() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        if (convertView==null) {
            convertView = layoutInflater.inflate(R.layout.group, parent,false);
        }
        TextView textView = (TextView) convertView.findViewById(R.id.tv_group);
        textView.setText(groupList.get(groupPosition));
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView==null) {
            convertView = layoutInflater.inflate(R.layout.child, null);
        }
        TextView textView = (TextView) convertView.findViewById(R.id.tv_child);
        textView.setText(childList.get(groupPosition).get(childPosition));
        return convertView;
    }
    /**
     * 返回值为true时   对child做出响应
     */
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}


转载于:https://my.oschina.net/u/2282721/blog/487280

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值