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