maxHeight does not work on RecyclerView

戏子无情 提交于 2019-12-06 06:35:00

问题


I've a DialogFragment.

Layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:maxHeight="280dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/line/>

    <View
        android:id="@+id/line"
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:background="@color/black"/>

</android.support.constraint.ConstraintLayout>

I want to initially "wrap recycler view content" (height), but it cannot exceed 280dp. It seems that android:maxHeight has no effect what-so-ever.


回答1:


You can achieve this using only XML as long as your parent layout is ConstraintLayout, which yours seems to be. Make these changes to your RecyclerView tag:

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintHeight_default="wrap"
    app:layout_constraintHeight_max="280dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/line/>

The key attributes are these three:

android:layout_height="0dp"
app:layout_constraintHeight_default="wrap"
app:layout_constraintHeight_max="280dp"

Normally you only use a 0dp dimension when you constrain both sides of the view, but really all you have to do is provide enough constraints to define the view size (and constraining both sides is just the easiest way to do that). Once you have the height set to "match constraints", you can combine a default height constraint with a maximum height constraint to get exactly what you're looking for.

Note you must be using a 1.1 version of the constraint layout library for this. Make sure your app's build.gradle file has something like:

implementation 'com.android.support.constraint:constraint-layout:1.1.0-beta5'



回答2:


I had the same problem. I extended the RecyclerViewas follows:

public class MaxHeightRecyclerView extends RecyclerView {
    private static final int MAX_HEIGHT = 765;

    public MaxHeightRecyclerView(Context context) {
        super(context);
    }

    public MaxHeightRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MaxHeightRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthSpec, int heightSpec) {
        heightSpec = MeasureSpec.makeMeasureSpec((int) (MAX_HEIGHT / Resources.getSystem().getDisplayMetrics().density), MeasureSpec.AT_MOST);
        super.onMeasure(widthSpec, heightSpec);
    }
}


来源:https://stackoverflow.com/questions/49449828/maxheight-does-not-work-on-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!