【问题标题】:RecyclerView Item does not wrap height of ListViewRecyclerView Item 不包裹 ListView 的高度
【发布时间】:2018-02-05 13:36:30
【问题描述】:

所以我有这个活动,看起来像这样:

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

如你所见,Activity 有一个 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 文件的这种行为。非常感谢您的帮助。

【问题讨论】:

    标签: java android xml listview android-recyclerview


    【解决方案1】:

    将此添加到您的列表视图...动态设置列表视图的高度

    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 适配器

    【讨论】:

    • 不是所有的英雄都穿斗篷!谢谢你,这很好用。
    • 救命稻草!谢谢!
    【解决方案2】:

    一种选择是使用垂直线性布局来放置页面的所有内容。然后将视图(包括recyclerview)的高度设置为0dp并添加布局权重。

    【讨论】:

      猜你喜欢
      • 2014-08-26
      • 2014-06-12
      • 2015-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多