簡體   English   中英

RecyclerView項目不包裝ListView的高度

[英]RecyclerView Item does not wrap height of ListView

所以我有這個活動,看起來像這樣:

<android.support.constraint.ConstraintLayout 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"
tools:context="com.example.jonas.trainingslog1.WorkoutDataActivity">


        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <android.support.constraint.ConstraintLayout
                ...
            </android.support.constraint.ConstraintLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toBottomOf="@+id/layout">

            </android.support.v7.widget.RecyclerView>


        </android.support.constraint.ConstraintLayout>

如您所見,該活動具有一個RecyclerView,其各項如下所示:

<android.support.constraint.ConstraintLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">


<ListView
    android:id="@+id/lst"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


</ListView>

<Button
    android:layout_width="match_parent"
    android:layout_height="30dp"
    app:layout_constraintTop_toBottomOf="@+id/lst"/>

RecyclerView項目中ListView的高度應僅與其內容一樣高,因此我將其設置為wrap_content 問題是,僅當我將RecyclerView項布局的高度設置為match_parent ,這才起作用,一旦將其更改為wrap_content ,ListView僅顯示一項。 我怎么不能將RecyclerView項目的高度保留為match_parent因為那時RecyclerView每個項目都有很多未使用的空間。

任何人如何解決此問題? 我不了解xml文件的這種行為。 我非常感謝您的幫助。

將此添加到您的listview ...動態設置listview的高度

public static void setListViewHeightBasedOnChildren(final ListView listView) {
listView.post(new Runnable() {
    @Override
    public void run() {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
        int listWidth = listView.getMeasuredWidth();
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(
                    View.MeasureSpec.makeMeasureSpec(listWidth, View.MeasureSpec.EXACTLY),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));


            totalHeight += listItem.getMeasuredHeight();
            Log.d("listItemHeight " + listItem.getMeasuredHeight(), "********");
        }

        Log.d("totalHeight " + totalHeight, "********");

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = (totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)));
        listView.setLayoutParams(params);
        listView.requestLayout();

    }
});
}

將此添加到您的recyclerview適配器

一種選擇是使用垂直線性布局,在其中放置頁面的所有內容。 然后將視圖(包括recyclerview)的高度設置為0dp並添加布局權重。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM