ExpandableListView的使用

ExpandableListView的使用

效果图

这里写图片描述

布局

<ExpandableListView
    android:id="@+id/expandableListView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

初始化

ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);

填充数据

KongqwExpandableListviewAdapter kongqwExpandableListviewAdapter = new KongqwExpandableListviewAdapter(this);
expandableListView.setAdapter(kongqwExpandableListviewAdapter);

Adapter

package com.example.kongqw.myapplication;

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

import java.util.ArrayList;

/**
 * Created by kongqw on 2015/12/21.
 */
public class KongqwExpandableListviewAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private ArrayList<String> mGroups;
    private ArrayList<String> mChilds;

    // 构造方法
    public KongqwExpandableListviewAdapter(Context context) {
        mContext = context;
        // 模拟初始化数据
        mGroups = new ArrayList<String>();
        mGroups.add("Group 1");
        mGroups.add("Group 2");
        mGroups.add("Group 3");
        mGroups.add("Group 4");
        mGroups.add("Group 5");
        mChilds = new ArrayList<String>();
        mChilds.add("Child 1");
        mChilds.add("Child 2");
        mChilds.add("Child 3");
        mChilds.add("Child 4");
        mChilds.add("Child 5");
        mChilds.add("Child 6");
        mChilds.add("Child 7");
        mChilds.add("Child 8");
        mChilds.add("Child 9");
        mChilds.add("Child 10");
    }

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

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

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mChilds.get(childPosition);
    }

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View view = View.inflate(mContext, R.layout.expandable_group_item, null);
        TextView textView = (TextView) view.findViewById(R.id.group_item);
        textView.setText(mGroups.get(groupPosition));
        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View view = View.inflate(mContext, R.layout.expandable_child_item, null);
        TextView textView = (TextView) view.findViewById(R.id.child_item);
        textView.setText(mChilds.get(childPosition));
        return view;
    }

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

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

去掉箭头

expandableListView.setGroupIndicator(null);

默认展开

// 设置ExpandableListView默认是展开的
for (int i = 0; i < kongqwExpandableListviewAdapter.getGroupCount(); i++) {
    expandableListView.expandGroup(i);
}

Group不可点击

expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
        // TODO Auto-generated method stub
        return true;
    }
});

TODO 复用

转载于:https://www.cnblogs.com/sesexxoo/p/6190479.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值