Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android maxHeight在RecyclerView上不起作用_Android_Android Recyclerview - Fatal编程技术网

Android maxHeight在RecyclerView上不起作用

Android maxHeight在RecyclerView上不起作用,android,android-recyclerview,Android,Android Recyclerview,我有一个对话框片段 布局: <?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="ma

我有一个
对话框片段

布局:

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


我也有同样的问题。我将回收服务扩展为以下内容:

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);
    }
}

只要父布局是
ConstraintLayout
,您就可以使用XML实现这一点,而您的父布局似乎是这样的。对
RecyclerView
标记进行以下更改:

<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/>
通常,当约束视图的两侧时,仅使用
0dp
标注,但实际上,您所要做的就是提供足够的约束来定义视图大小(约束两侧只是最简单的方法)。将高度设置为“匹配约束”后,可以将默认高度约束与最大高度约束相结合,以精确获得所需的高度

注意:您必须为此使用1.1版本的约束布局库。确保应用程序的build.gradle文件包含以下内容:

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

我真的很挣扎。如果使用androidx,请注意以下几点: -首先使用最新的依赖项,Im使用 实现“androidx.constraintlayout:constraintlayout:2.0.0-beta4” -请注意,在androidx中,标记有点不同

<androidx.constraintlayout.widget.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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_tilemap"
android:layout_width="match_parent"
android:layout_height="wrap_content">


    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintHeight_default="wrap"
        app:layout_constraintHeight_max="280dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:id="@+id/rv_comms"
        android:padding="8dp"
        android:clipToPadding="false" />

</androidx.constraintlayout.widget.ConstraintLayout>


这就是最终对我有效的方法。

如果您想使用XML文件实现这一点,以下是步骤。 这是针对androidx组件的。首先,您需要在gradle文件中实现一个beta版本

implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta6'
在此之后,使用以下代码创建最大高度为240dp的recyclerView

 <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
   >
    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintHeight_default="wrap"
        app:layout_constraintHeight_max="240dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
 </androidx.constraintlayout.widget.ConstraintLayout>

试试这个。给那些没有参与预览的人答案

经过一些尝试和错误。尝试添加app:layout\u constraintHeight\u min=“100dp”



使用
app:layout\u constraintHeight\u default=“280dp”
@HemantParmar您不能将“280dp”设置为
app:layout\u constraintHeight\u default
我希望使用xml实现此功能。谢谢您的回答。获得的知识
layout\u constraintHeight\u default=“wrap”
不推荐使用。改为使用
layout\u height=“WRAP\u CONTENT”
layout\u constrained height=“true”
。谢谢,我不得不升级到2.x.x。
 <androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
   >
    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintHeight_default="wrap"
        app:layout_constraintHeight_max="240dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
 </androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHeight_default="wrap"
            app:layout_constraintHeight_max="400dp"
            app:layout_constraintHeight_min="100dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:listitem="@layout/list_item" />

</androidx.constraintlayout.widget.ConstraintLayout>