当前位置:网站首页>Treatment of incomplete display of listview height

Treatment of incomplete display of listview height

2022-04-23 18:42:00 Xiao Xiaofeng

listview Show incomplete scene :

1、listview(scrollview...) nesting listview, Inside listview Incomplete height display

2、 The dynamic change listview Of item Layout ,listview Incomplete height display


Handle :

stay listview After adding an adapter , Dynamic computing listview Height and set the height .

The method is as follows :

/**
 *  Dynamic setting listview Height 
 *
 * @param listView
 * @param h
 */
public static void setListViewHeightBasedOnChildren(ListView listView) {

    //  obtain ListView Corresponding Adapter

    ListAdapter listAdapter = listView.getAdapter();

    if (listAdapter == null) {
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = 0;
        listView.setLayoutParams(params);
        return;
    }

    int totalHeight = 0;

    for (int i = 0; i < listAdapter.getCount(); i++) { // listAdapter.getCount() Returns the number of data items 

        View listItem = listAdapter.getView(i, null, listView);

        listItem.measure(0, 0); //  Calculate the children View  The width and height of 

        totalHeight += listItem.getMeasuredHeight(); //  Count the total height of all subitems 

    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();

    params.height = totalHeight
            + (listView.getDividerHeight() * (listAdapter.getCount() - 1))
            + listView.getPaddingBottom() + listView.getPaddingTop();

    // listView.getDividerHeight() Gets the height occupied by the separator between children 

    // params.height And finally get the whole ListView Full display of required height 

    listView.setLayoutParams(params);

}


If the above still cannot be solved , Please check listview Of item Layout :

The height of the outermost control must be set to The adaptive (wrap_content) or fill (match_parent

example :

Original layout :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="148px"
 android:gravity="center_vertical"
 android:orientation="horizontal"
 android:paddingLeft="50px"
 android:paddingRight="50px">

<TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" 
android:text=" name " android:textSize="44px" />

 <TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginRight="50px"
 android:layout_weight="1"
 android:drawablePadding="50px"
 android:drawableRight="@drawable/ic_crumb"
 android:gravity="right"
 android:text=""
 android:textSize="44px" />
</LinearLayout>


It is amended as follows :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="148px"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:paddingLeft="50px"
    android:paddingRight="50px">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=" name "
        android:textSize="44px" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="50px"
        android:layout_weight="1"
        android:drawablePadding="50px"
        android:drawableRight="@drawable/ic_crumb"
        android:gravity="right"
        android:text=""
        android:textSize="44px" />
</LinearLayout>

</LinearLayout>








版权声明
本文为[Xiao Xiaofeng]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210604091385.html