AndroidX RecyclerView行高不会换行

标签 android android-recyclerview androidx

我一直在使用新的 Androidx 库,但遇到了无法修复的问题。我正在使用 RecyclerView,但无法让行换行。它应该能够做到这一点,而我读到的所有内容似乎都表明,如果我做不好它就会起作用。我以前用过这个,没有出现这个问题。我尝试调整主布局和项目布局中的每个设置,但似乎没有什么影响它。任何帮助将不胜感激。

ma​​in_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
    xmlns:tools="http://schemas.android.com/tools">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/cardRecycler"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
            android:clipToPadding="false"
            app:spanCount="4"

            android:orientation="vertical"
            tools:listitem="@layout/item_card_preview"/>

    </androidx.core.widget.NestedScrollView>

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:liftOnScroll="true">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@android:color/white"
            app:titleTextColor="@color/colorSecondary" />

    </com.google.android.material.appbar.AppBarLayout>


</androidx.coordinatorlayout.widget.CoordinatorLayout>

item.xml

 <?xml version="1.0" encoding="utf-8"?>
    <layout 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">

        <data>
            <variable
                name="card"
                type="com.jibmobile.clashroyaltoolkit.vo.CardPreview"/>


        </data>

        <ImageView
            android:id="@+id/imageView"
            image="@{card.name}"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="0dp"
            android:paddingTop="0dp"
            android:contentDescription="@{card.displayName}"
            tools:src="@drawable/archers" />

最佳答案

就我而言,在 item.xml 中,高度被指定为“match_parent”。当我将其转换为“wrap_content”后,回收 View 的高度根据项目的数量而扩展。请注意,我也在使用 androidx 库。

item.xml代码是这样的

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:padding="@dimen/small_padding">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/tickred"
        android:layout_marginStart="0dp"
        android:layout_marginTop="1dp"
        android:layout_gravity="top"
        android:layout_marginEnd="@dimen/dimen_margin_between_text"/>

    <androidx.appcompat.widget.AppCompatTextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:id="@+id/serviceTv"
        android:layout_gravity="top"
        android:gravity="top"
        app:fontFamily="@font/lato_regular"
        android:textColor="@color/black"
        android:textSize="@dimen/dimen_text_smallx"/>

</LinearLayout>

我将容器高度更改为wrap_content,并且recycleview相应地采用其高度。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:padding="@dimen/small_padding">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/tickred"
        android:layout_marginStart="0dp"
        android:layout_marginTop="1dp"
        android:layout_gravity="top"
        android:layout_marginEnd="@dimen/dimen_margin_between_text"/>

    <androidx.appcompat.widget.AppCompatTextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:id="@+id/serviceTv"
        android:layout_gravity="top"
        android:gravity="top"
        app:fontFamily="@font/lato_regular"
        android:textColor="@color/black"
        android:textSize="@dimen/dimen_text_smallx"/>

</LinearLayout>

关于AndroidX RecyclerView行高不会换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51699534/

相关文章:

android - 我收到错误 "No instrumentation registered! Must run under a registering instrumentation"

kotlin - 如何设置 RecyclerView 中的属性在 ViewModel 列表项属性更新时更新?

java - 在双向 Recyclerview 中处理水平滚动时禁用 SwipeRefreshLayout

android - 房间存储库 : Selecting one row by ID

android - 如何缩放位图?

安卓| RecyclerView 项目上的 AlertDialog 单击

android - 找不到满足版本限制的 'androidx.arch.core:core-common' 版本

android - fontFamily 无法在 androidx 上运行

android - 线性渐变在移动网络浏览器中不起作用

java - 如何知道一个应用程序在最近使用很长时间后何时被杀死?