ExpandableListView的使用

布局文件中

<ExpandableListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:cacheColorHint="#00000000"
    android:descendantFocusability="afterDescendants"
    android:divider="@color/white" --》group的divider
    android:dividerHeight="1px" --> divider的高度,对child也生效
    android:childDivider="@color/white" --》 child的divider
    android:fastScrollEnabled="false"
    android:groupIndicator="@null"    --》 去掉默认的箭头,方便自己定义展开的图片。
    android:listSelector="@android:color/transparent"
    android:scrollbars="none" >
</ExpandableListView>

代码中使用:
private ExpandAdapter mAdapter;
mAdapter = new ExpandAdapter(mContext,objectList);
mListView.setAdapter(mAdapter);

Adapter定义 继承BaseExpandableListAdapter
public class ExpandAdapter extends BaseExpandableListAdapter {
    private List<Object> groupList; // 这里为对象模型列表,每个列表中需包含childview对象的列表
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    public ExpandAdapter (Context context, List<Object> objects) {
        mContext = context;
        groupList = objects;
        mLayoutInflater = LayoutInflater.from(context);
    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        if (groupList.get(groupPosition) != null && groupList.get(groupPosition).getChildList()!= null) {
            return groupList.get(groupPosition).getChildList().size();
        } else {
            return 0;
        }
    }

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

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

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

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

    @Override
    public boolean hasStableIds() {
        return false;
    }
// group的布局显示,每个组的布局
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new GroupViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.item_groupview, null);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (GroupViewHolder) convertView.getTag();
        }
        // TODO
        if (isExpanded) {
            //展开了,如果需要显示自定义的箭头
        } else {
            //未展开,如果需要显示自定义的箭头

        }
        return convertView;
    }
// 组的字列表布局
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ChildViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.item_childview, null);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ChildViewHolder) convertView.getTag();
        }
        // TODO
        
        return convertView;
    }

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

    class GroupViewHolder {
       // TODO
    }

    class ChildViewHolder {
        // TODO
    }
}


实现了上面的方法就可以显示ExpandablListView了


展开listview某项

exListView.expandGroup(groupPosition);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值