【问题标题】:Android - RecyclerView/Gridview height is stretchingAndroid - RecyclerView/Gridview 高度正在拉伸
【发布时间】:2020-11-16 23:48:33
【问题描述】:

只是想创建一个简单的网格 2 行视图,如屏幕,但我所有嵌套视图的高度都在拉伸。有点像它伸展到屏幕的高度。我不是 100% 为什么会发生这种情况。提前致谢!

这是我的 MainActivity:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        loadAdapter(listOf("URL_1", "URL_2"))
    }

    private fun loadAdapter(images: List<String>){
        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        recyclerView?.layoutManager = GridLayoutManager(this, 2)
        recyclerView?.adapter = GridViewAdapter(this, images)
    }
}

我的适配器:

class GridViewAdapter(
    private val context: Context,
    private val dataSet: List<String>
) :
    RecyclerView.Adapter<GridViewAdapter.ViewHolder>() {

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val imageView: ImageView = view.findViewById(R.id.imageView)

        init {

        }
    }

    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(viewGroup.context)
            .inflate(R.layout.view_image, viewGroup, false)

        return ViewHolder(view)
    }

    override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
        Glide.with(context).load(dataSet[position]).into(viewHolder.imageView)
    }

    override fun getItemCount() = dataSet.size

}

recyclerview 视图单元格:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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:cardBackgroundColor="@android:color/transparent"
    android:layout_margin="8dp"
    app:layout_constraintDimensionRatio="H,1:1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintDimensionRatio="H,1:1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@tools:sample/avatars" />
</androidx.cardview.widget.CardView>

MainActivity 布局:

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

    标签: gridview android-recyclerview layout adapter


    【解决方案1】:

    在官方 Glide 文档中实际上有一个关于使用 Glide 和 Recyclerview 的部分:https://bumptech.github.io/glide/int/recyclerview.html

    他们建议使用PreloadSizeProvider 来确保单元格的大小正确。

    我在每行显示 3 个平方图像时遇到了同样的问题,这意味着单元格高度受单元格宽度的限制。当我提供占位符(颜色)时,问题就消失了。

    Glide.with(imageView).load(uri).placeholder(R.color.white).into(imageView)
    

    在我看来,设置一个(无量纲)占位符,高度是根据宽度计算的(正如预期的那样,它应该是一个正方形)。加载图像后,它将被放入单元格并根据单元格的尺寸调整大小。我认为这更像是一种解决方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      • 1970-01-01
      • 2014-10-14
      • 1970-01-01
      • 2015-09-03
      • 2017-08-24
      • 2014-03-10
      相关资源
      最近更新 更多