Android 多级列表嵌套笔记

一. 重写ExpandableListView

/**
 *
 * 自定义ExpandableListView  解决嵌套之下显示不全的问题
 */
public class CustomExpandableListView extends ExpandableListView {


    public CustomExpandableListView(Context context) {
        super(context);
    }

    public CustomExpandableListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public CustomExpandableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 解决显示不全的问题
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2
                , MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}

二. 修改ListViewAdapter

  1. getChildrenCount() 返回1
  2. getChildView() 返回的是ExpandableListView对象。
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        CustomExpandableListView eListView = getExpandableListView();
        List<Item> childData = new ArrayList<Item>();
        childData.addAll(getChildData(groupPosition));
        // 嵌套第三层的适配器
        PackageChildListAdapter packageChildListAdapter = new PackageChildListAdapter(mContext, childData);
        eListView.setAdapter(packageChildListAdapter);
        return eListView;
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 对象的创建与销毁 Item 1: 使用static工厂方法,而不是构造函数创建对象:仅仅是创建对象的方法,并非Fa...
    孙小磊阅读 6,210评论 0 3
  • 《峄山碑》文: 皇帝立国,维初在昔,嗣世称王 讨伐乱逆,威动四极,武义直方 戎臣奉詔,經時不久,滅六暴強 廿有六年...
    双髻山府正堂阅读 2,701评论 1 10
  • 对于“膳食纤维”大家一定不陌生,从1953年Hipsley教授首次提出膳食纤维(Dietary Fiber,DF)...
    公寓门前外阅读 3,520评论 0 0
  • 清晨总能听见鸟儿的叫声,因为树枝上有一个大大的鸟巢,那是鸟儿栖息的地方,欢快地鸟叫声,和鸟儿一飞而过的瞬间...
    清香百合1124阅读 4,470评论 6 14