Android - Listviews



我想使我的listViews视为实际内容的大小。例如,如果有20个项目,我希望它显示20个项目,而不会像现在这样滚动列表本身。

看起来像ATM:列表很小,它们滚动。https://i.gyazo.com/eb8902a17936f54a2271b51abe228607.png

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.tiagosilva.amob_android.ArchiveFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="20dp">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Tube Data"
            android:textColor="@color/AMOB_yellow"/>
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tubeData_list">
    </ListView>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Tool Data"
            android:textColor="@color/AMOB_yellow"/>
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/toolData_list">
    </ListView>
        <Button
            android:id="@+id/btn_clear_all"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/round_buttons"
            android:text="Clear all"
            android:textColor="@color/AMOB_gray"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp"
            android:gravity="center"/>
    </LinearLayout>
  </ScrollView>

尝试使用它,可能是您必须解决的解决方案: -

 Utility.setListViewHeightBasedOnChildren(yourlistview);

添加此方法: -

public static class Utility {
        public static void setListViewHeightBasedOnChildren(ListView listView) {
            ListAdapter listAdapter = listView.getAdapter();
            if (listAdapter == null) {
                // pre-condition
                return;
            }
            int totalHeight = 0;
            for (int i = 0; i < listAdapter.getCount(); i++) {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }
            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
            listView.setLayoutParams(params);
        }
    }

它不会滚动,但是您可以根据需要获得实际内容的大小。

最新更新