简体   繁体   English

RecyclerView项目不包装ListView的高度

[英]RecyclerView Item does not wrap height of ListView

So I have this activity, which looks like this: 所以我有这个活动,看起来像这样:

<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>

As you can see, the activity has an RecyclerView which items look like this: 如您所见,该活动具有一个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"/>

The Height of the ListView in the RecyclerView Item should only be as tall as its content, so I set it to wrap_content . RecyclerView项目中ListView的高度应仅与其内容一样高,因此我将其设置为wrap_content The problem is, this only works as long as I have set the height of the RecyclerView Item Layout to match_parent , as soon as I change it to wrap_content the ListView shows only one item. 问题是,仅当我将RecyclerView项布局的高度设置为match_parent ,这才起作用,一旦将其更改为wrap_content ,ListView仅显示一项。 How ever I cant leave the height of the RecyclerView Item as match_parent because then every item of the RecyclerView has a lot of unused space. 我怎么不能将RecyclerView项目的高度保留为match_parent因为那时RecyclerView每个项目都有很多未使用的空间。

Anyone hows how to fix this problem? 任何人如何解决此问题? I dont understand this behavior of the xml files. 我不了解xml文件的这种行为。 I would really appreciate help. 我非常感谢您的帮助。

Add this to you listview...set the height of the listview dynamically 将此添加到您的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();

    }
});
}

add this to your recyclerview adapter 将此添加到您的recyclerview适配器

One option is to use a vertical linear layout where you put all your content of the page. 一种选择是使用垂直线性布局,在其中放置页面的所有内容。 Then set the height of the views (including the recyclerview) to 0dp and add a layout weight. 然后将视图(包括recyclerview)的高度设置为0dp并添加布局权重。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM