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

前端 未结 12 982
小蘑菇
小蘑菇 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:52

    I took the function from djunod's answer (the function in static class)

    calculate listview size

    and change ondraw method as follow. it works after delete operations successfully.

    here is my final class

    class ExpandedListView extends ListView {

    private android.view.ViewGroup.LayoutParams params;
    private int old_count = 0;
    
    public ExpandedListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
    
        if (getCount() != old_count) {
            params = getLayoutParams();
            old_count = getCount();
            int totalHeight = 0;
            for (int i = 0; i < getCount(); i++) {
                this.measure(0, 0);
                totalHeight += getMeasuredHeight();
            }
    
            params = getLayoutParams();
            params.height = totalHeight + (getDividerHeight() * (getCount() - 1));
            setLayoutParams(params);
        }
    
        super.onDraw(canvas);
    }
    

    }

    now my listview expands after row insert without scrolling. and after row delete this code calculates the correct size for listview. i don't know how it is working. but it is working. BR

提交回复
热议问题