如何为多级可扩展列表视图提供动态高度?

时间:2015-09-15 08:52:59

标签: android expandablelistview expandablelistadapter

我使用expandablelist3代码示例中的以下代码进行我的2级可扩展但问题是我无法设置确切的动态高度,并且两个可扩展项目之间的差距很大。请帮我设置一个动态高度

enter image description here 这些是我的代码: 第二级可扩展列表视图

public class DebugExpandableListView extends ExpandableListView {
public DebugExpandableListView(Context context) {
    super(context);
}

public void setRows(int rows) {
    this.rows = rows;
    Log.d(LOG_TAG, "rows set: " + rows);
}

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension(getMeasuredWidth(), rows * ROW_HEIGHT);
    Log.d(LOG_TAG, "onMeasure " + this +
            ": width: " + decodeMeasureSpec(widthMeasureSpec) +
            "; height: " + decodeMeasureSpec(heightMeasureSpec) +
            "; measuredHeight: " + getMeasuredHeight() +
            "; measuredWidth: " + getMeasuredWidth());
}

protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    Log.d(LOG_TAG, "onLayout " + this + ": changed: " + changed + "; left: " + left + "; top: " + top + "; right: " + right + "; bottom: " + bottom);
}

private String decodeMeasureSpec(int measureSpec) {
    int mode = View.MeasureSpec.getMode(measureSpec);
    Log.e("ex view mode", String.valueOf(mode));
    String modeString = "<> ";
    switch (mode) {
        case View.MeasureSpec.UNSPECIFIED:
            modeString = "UNSPECIFIED ";
            break;

        case View.MeasureSpec.EXACTLY:
            modeString = "EXACTLY ";
            break;

        case View.MeasureSpec.AT_MOST:
            modeString = "AT_MOST ";
            break;
    }
    return modeString + Integer.toString(View.MeasureSpec.getSize(measureSpec));
}

private static final int ROW_HEIGHT = 110;
private static final String LOG_TAG = "DebugExpandableListView";
private int rows;
}

二级可扩展列表视图适配器

 public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService
                (Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.child3_row, parent, false);
    }

    imageLink = (ImageView) v.findViewById(R.id.iv_item_image);
    TextView itemCode = (TextView) v.findViewById(R.id.tv_item_code);
    TextView itemName = (TextView) v.findViewById(R.id.childname);
    TextView itemDesc = (TextView) v.findViewById(R.id.tv_item_desc);
    TextView itemRate = (TextView) v.findViewById(R.id.rgb);

    item = (HashMap<String, String>) ((List<HashMap>) childData.get
            (groupPosition)).get
            (childPosition);

    itemCode.setText(item.get(Constants.KEY_CODE));
    itemName.setText(item.get(Constants.KEY_SHADENAME));
    itemDesc.setText(item.get(Constants.KEY_DESC));
    itemRate.setText(item.get(Constants.KEY_RGB));

    Picasso.with(context)
            .load(Constants.WEB_SERVICE_BASE_URL + "/ROItem/images/" + item.get(Constants
                    .KEY_IMG_LNK))
            .resize(100, 100)
            .into(imageLink);

    return v;

}

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    View v = super.getGroupView(groupPosition, isExpanded, convertView, parent);
    Log.d(LOG_TAG, "getGroupView: groupPosition: " + groupPosition + "; isExpanded: " + isExpanded + "; v: " + v);
    return v;

}

这是宣言

 DebugExpandableListView dev = new DebugExpandableListView( context );
        dev.setRows(calculateRowCount(groupPosition, null));
        final List list=createChildList( groupPosition );
        dev.setAdapter(
                new DebugSimpleExpandableListAdapter(
                        context,
                        createGroupList(groupPosition),    // groupData describes the first-level entries
                        R.layout.group2_row,    // Layout for the first-level entries
                        new String[]{Constants.KEY_COLORNAME},    // Key in the groupData maps to display
                        new int[]{R.id.groupname},        // Data under "colorName" key goes into this TextView
                        list,    // childData describes second-level entries
                        R.layout.child3_row,    // Layout for second-level entries
                        new String[]{Constants.KEY_SHADENAME, Constants.KEY_RGB},    // Keys in childData maps to display
                        new int[]{R.id.childname, R.id.rgb}    // Data under the keys above go
                        // into these TextViews
                )
        );

  private List createGroupList( int level1 ) {
    ArrayList result = new ArrayList();
    for( int i = 0 ; i < listdesc.get(level1).getCategoryList().size() ; ++i ) {
        HashMap m = new HashMap();
        m.put(Constants.KEY_COLORNAME, listdesc.get(level1).getCategoryList().get(i).getCategoriesName());
        result.add( m );

    }
    return (List)result;
}


private List createChildList( int level1 ) {
    ArrayList result = new ArrayList();
    for( int i = 0 ; i < listdesc.get(level1).getCategoryList().size() ; ++i ) {

        Log.e("i=",String.valueOf(i));
        ArrayList secList = new ArrayList();
        for( int n = 0 ; n < listdesc.get(level1).getCategoryList().get(i).getItemList()
                .size()
                ; n++ ) {
            Log.e("n=", String.valueOf(n));
            HashMap child = new HashMap();
            child.put( Constants.KEY_SHADENAME, listdesc.get(level1).getCategoryList().get(i)
                    .getItemList().get(n).getItemName());
            child.put(Constants.KEY_RGB, String.valueOf(listdesc.get(level1).getCategoryList().get(i)
                    .getItemList().get(n).getPrice()));
            child.put( Constants.KEY_DESC, "Have a nice dine in our place!!!");
            child.put(Constants.KEY_CODE,  listdesc.get(level1).getCategoryList().get(i)
                    .getItemList().get(n).getItemCode());
            child.put(Constants.KEY_IMG_LNK, String.valueOf(listdesc.get(level1).getCategoryList().get(i)
                    .getItemList().get(n).getPhotoPath()) );

            secList.add( child );

        }
        result.add( secList );
    }

    return result;
}

private int calculateRowCount( int level1, ExpandableListView level2view ) {
    int level2GroupCount = listdesc.get(level1).getCategoryList().size();
    int rowCtr = 0;
    for( int i = 0 ; i < level2GroupCount ; ++i ) {
        ++rowCtr;      
        if( ( level2view != null ) && ( level2view.isGroupExpanded( i ) ) ) {
            rowCtr += listdesc.get(level1).getCategoryList().get(i).getItemList().size();
        }

    }
    return rowCtr;
}

 public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

        if( parent.isGroupExpanded( groupPosition ) )
            parent.collapseGroup( groupPosition );
        else
         parent.expandGroup( groupPosition );
        if( parent instanceof DebugExpandableListView ) {
            DebugExpandableListView dev = (DebugExpandableListView)parent;
            dev.setRows( calculateRowCount( level1GroupPosition, parent ) );
        }
        Log.d( LOG_TAG, "onGroupClick" );
        topExpList.requestLayout();
        return true;
    }

0 个答案:

没有答案