【问题标题】:Staggered LayoutManager showing images in only one column交错的 LayoutManager 仅在一列中显示图像
【发布时间】:2020-05-31 17:48:13
【问题描述】:
我正在使用带有 kotlin 的交错布局管理器将本地手机存储中的图像和视频设置为 recyclerview。一切都很好,但在第一次启动时,recyclerview 仅在一列中显示所有图像和视频(仅在右侧),在 recyclerview 上引用一切正常。我已设置跨度计数 2 并使用滑行。我是新手,在此先感谢。
这是一些代码。
recyclerview?.setHasFixedSize(true)
recyclerview.layoutManager = StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)
adapter = ImageAdapter(activity)
recyclerview?.adapter = adapter
adapter?.notifyDataSetChanged()
Layout for recyclerview:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter" />
</RelativeLayout>
【问题讨论】:
标签:
android
kotlin
android-recyclerview
staggeredgridlayout
【解决方案1】:
尝试将宽度和高度从 match_parent 更改为 wrap_content :
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/img"
android:layout_width="100dp"
android:layout_height="180dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter" />
</RelativeLayout>
顺便说一句,这就是我的工作方式:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rv_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/rv_image"
android:layout_width="match_parent"
android:layout_height="278dp"
android:layout_margin="0dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>