Calculate the size of a list view or how to tell it to fully expand

前端 未结 12 948
小蘑菇
小蘑菇 2020-11-29 00:20

I am currently trying to use a ListView inside of a ScrollView. I know from what I\'ve read that this is looked down upon, but I\'m trying to get the ListView to expand com

12条回答
  •  情深已故
    2020-11-29 00:47

    I've been researching on how to do this, and although the question has already been answered, I'd like to provide a solution that I think is better:

    Looking at the ListView's source code you can see the following inside the onMeasure(...) code:

    if (heightMode == MeasureSpec.AT_MOST) {
         // TODO: after first layout we should maybe start at the first visible position, not 0
         heightSize = measureHeightOfChildren(widthMeasureSpec, 0, NO_POSITION, heightSize, -1);
    }
    

    So simply call the listView's measure and pass MeasureSpec.AT_MOST for the height instead of the usual MeasureSpec.UNSPECIFIED.

    I do the following to measure a listview and place it inside a popup-window:

        int widthMeasureSpec = MeasureSpec.makeMeasureSpec(displayMetrics.widthPixels, MeasureSpec.EXACTLY);
        int heightMeasureSpec = MeasureSpec.makeMeasureSpec(displayMetrics.heightPixels, MeasureSpec.AT_MOST);
        mCatalogView.measure(widthMeasureSpec, heightMeasureSpec);
    
        mPopup.setWidth(mCatalogView.getMeasuredWidth());
        mPopup.setHeight(mCatalogView.getMeasuredHeight());
    

提交回复
热议问题