Android 動態計算ListView的高度

一、簡介


在Android開發的過程中有的時候我們需要手動計算ListView的高度,比如說,ScrollView中嵌套ListView的時候,我們就需要手動精確計算ListView的高度了。

如果ListView的Item高度是固定的話還好計算一些,我們可以直接使用Item的條數 * Item的固定高度來計算,但是如果Item的高度隨着內容的變化而變化,那麼該如何計算呢?

下面我們就開始說說如何精確計算ListView的高度吧。

二、效果圖


先看下界面效果:
在這裏插入圖片描述
從效果圖中我們可以看到:

紅色背景的是Item,藍色背景的是ListView的dividerHeight的高度,同時我們也設置了ListView的paddingTop和paddingBottom值。

三、代碼實現


下面我們就直接上代碼:

1、Item的佈局文件list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txt_item_info"
        android:gravity="center"
        android:textColor="#ffffff"
        android:padding="20dp"
        android:textSize="18sp"
        android:lineSpacingExtra="10dp"
        android:text="測試一"
        android:background="@color/colorAccent"/>

</LinearLayout>

Item佈局文件中就定義了一個TextView,TextView的高度隨着內容的變化而變化。

2、ListView界面的佈局文件activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:background="#30B8E3"
        android:textColor="#ffffff"
        android:textSize="18sp"
        android:text="動態計算ListView高度"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/btn_add"
        android:text="添加Item"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ListView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/listview"
                android:divider="@color/colorPrimaryDark"
                android:dividerHeight="10dp"
                android:paddingTop="10dp"
                android:paddingBottom="10dp"
                android:cacheColorHint="#00000000"
                android:listSelector="#00000000"
                android:background="#ffffff"
                android:orientation="vertical">

            </ListView>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="#30B8E3"
                android:textColor="#ffffff"
                android:gravity="center"
                android:text="檢測ListView高度是否精確"/>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

這裏我們設置了ListView的dividerHeight、paddingTop、paddingBottom。


3、ListView高度計算


佈局文件準備好後,我們就來看下最關鍵的地方,動態計算ListView的高度,這裏我們只貼出計算ListView高度的代碼:

	public void setListViewHeight(ListView listview){
        ListAdapter adapter = listview.getAdapter();
        if(adapter == null){
            return;
        }

        int totalHeight = 0;
        // 計算ListView的寬度
        int listViewWidth = ((Activity)mContext).getWindowManager().getDefaultDisplay().getWidth();
        int widthSpec = View.MeasureSpec.makeMeasureSpec(listViewWidth, View.MeasureSpec.AT_MOST);

        for(int i=0;i<adapter.getCount();i++){
            View view = adapter.getView(i, null, listview);
            // 這裏的第一個參數必須使用widthSpec,
            // 如果使用0的話,無法計算出隨內容變化而變化的Item的真正高度值
            view.measure(widthSpec, 0);
            totalHeight += view.getMeasuredHeight();
        }

        int dividerHeight = listview.getDividerHeight() * (adapter.getCount() - 1);
        totalHeight += dividerHeight;
        Log.i("ListViewHeight", "ListView DividerHeight : " + dividerHeight);

        int paddingHeight = listview.getPaddingTop() + listview.getPaddingBottom();
        totalHeight += paddingHeight;
        Log.i("ListViewHeight", "ListView PaddingHeight : " + paddingHeight);

        Log.i("ListViewHeight", "ListView TotalHeight : " + totalHeight);
        ViewGroup.LayoutParams layoutParams = listview.getLayoutParams();
        layoutParams.height = totalHeight;
        listview.setLayoutParams(layoutParams);

        this.refresh();
    }

其中,最關鍵的地方就是下面這幾行代碼:

	// 計算ListView的寬度
    int listViewWidth = ((Activity)mContext).getWindowManager().getDefaultDisplay().getWidth();
    int widthSpec = View.MeasureSpec.makeMeasureSpec(listViewWidth, View.MeasureSpec.AT_MOST);


	// 這裏的第一個參數必須使用widthSpec,
    // 如果使用0的話,無法計算出隨內容變化而變化的Item的真正高度值
    view.measure(widthSpec, 0);

完整代碼已上傳至Github:動態計算ListView高度

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章