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 childData = new ArrayList();
        childData.addAll(getChildData(groupPosition));
        // 嵌套第三层的适配器
        PackageChildListAdapter packageChildListAdapter = new PackageChildListAdapter(mContext, childData);
        eListView.setAdapter(packageChildListAdapter);
        return eListView;
    }

你可能感兴趣的:(Android 多级列表嵌套笔记)