在Android中动态设置Listview Height

时间:2019-03-20 14:38:35

标签: android listview

我试图动态设置listview的高度,因为我想在不滚动的情况下滚动listview。它在nestedscrollview内部滚动,并且listview的项目高度也已正确设置。在我的实用程序类

hei = Double.valueOf(params.height / (6.25)).intValue();

我正在计算列表视图高度,然后将其设置为卡片视图高度。但是在这里,所有设备的速率改变为6.25,例如5.25 6.5等。在网上我发现params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));但是当我尝试此操作时,列表视图高度x 6或类似那。我怎样才能做到这一点?对不起我的英语不好。预先感谢。

我的布局:

    <androidx.core.widget.NestedScrollView
        android:id="@+id/nestedScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="55dp"
        android:fillViewport="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar">

<com.google.android.material.card.MaterialCardView
                    android:id="@+id/tankdatamcv"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginBottom="8dp"
                    app:cardCornerRadius="10dp"
                    app:cardPreventCornerOverlap="false"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/tankwave_layout">

                    <androidx.constraintlayout.widget.ConstraintLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

     <ListView
                            android:id="@+id/tankdetail_listview"
                            android:layout_width="0dp"
                            android:layout_height="0dp"
                            android:layout_centerHorizontal="true"
                            android:layout_marginTop="8dp"
                            android:background="@android:color/white"
                            android:nestedScrollingEnabled="false"
                            android:scrollbars="none"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintStart_toStartOf="parent"
                           app:layout_constraintTop_toBottomOf="@+id/tank_linechart" />

    </androidx.constraintlayout.widget.ConstraintLayout>

                </com.google.android.material.card.MaterialCardView>

 </androidx.core.widget.NestedScrollView>

java代码:用于度量列表视图项目的高度。

 class Utility {
        public void setListViewHeightBasedOnChildren(ListView listView) {
            ListAdapter listAdapter = listView.getAdapter();
            if (listAdapter == null) {
                return;
            }

            int totalHeight = 0;
            int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST);
            Log.e("getcount", "" + listAdapter.getCount());
            for (int i = 0; i < listAdapter.getCount(); i++) {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
                // listItem.measure(0, 0);
                Log.e("measure", "" + listItem.getMeasuredHeight());
                totalHeight += listItem.getMeasuredHeight();
                Log.e("total", "" + totalHeight);
               /* if (i == listAdapter.getCount() - 1) {
                    totalHeight += listItem.getMeasuredHeight() / 2;
                }*/
            }

            ViewGroup.LayoutParams params = listView.getLayoutParams();
            // params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
            // params.height = totalHeight;
            //params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
            params.height = totalHeight;
            hei = Double.valueOf(params.height / (6.25)).intValue();
            params.height = hei;
            Log.e("ichei", "" + params.height);
            listView.setLayoutParams(params);
            listView.requestLayout();
        }
    }

更改卡视图高度以更改listview高度:

 ViewTreeObserver observer = tankdetail_listview.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        // Log.e("hei", "" + hei);
                        tankdatamcv.getLayoutParams().height = hei;
                        tankdatamcv.requestLayout();
                    }
                });

0 个答案:

没有答案