如何设置 RecyclerView 项目相对于父项的最大高度 [英] How to set RecyclerView item max height relative to parent

查看:30
本文介绍了如何设置 RecyclerView 项目相对于父项的最大高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照这个问题中的建议,我几乎已经完成了这项工作

I've almost got this working by following the suggestions in this question

RecyclerView 项中的 Android 屏幕宽度百分比

然而,这为所有视图设置了高度,无论内容如何,​​高度都相同.就我而言,我只想限制 RecyclerView 中的项目与其高度相关的高度.

However that sets a height for all views, same height regardless of the content. In my case I just want to limit how tall an item inside the RecyclerView can be related to its height.

在我的房车中,我有不同高度的物品,其中一些物品甚至比房车还高.

In my RV I have items that have different heights, with some of those items capable of being even taller than the RV.

我的目标是告诉项目最多可以达到 RecyclerView 高度的 60%,到目前为止,我发现实现这一目标的唯一方法是每当有新项目时手动设置最大高度使用此逻辑绑定到视图持有者:

My goal is to tell that the item can be at max 60% of the RecyclerView height and so far the only way I found to achieve this is to set the max height manually whenever a new item is bound to the view holder using this logic:

constraintSet.constrainMaxHeight(viewId, (int) (recyclerView.getMeasuredHeight() * 0.6));

其中 recyclerView 是对托管 RecyclerView 的引用,我使用适配器的方法 onAttachedToRecyclerViewviewId 传递给适配器 是 RV 项 ConstraintLayout 根视图中的主视图,我可以使用它来控制它可以采用的最大高度.

Where recyclerView is a reference to the hosting RecyclerView that I pass to the adapter using the adapter's method onAttachedToRecyclerView and the viewId is the main view inside the RV item ConstraintLayout root view with which I control the max height it can take.

虽然这适合我的需要,但我想为此找到一个更简单/更优化的解决方案,在简单性方面类似于这个:https://stackoverflow.com/a/51224889/524695

Althought this works for what I need, I'd like to find a simpler/more optimized solution for this, similar to this one in terms of simplicity: https://stackoverflow.com/a/51224889/524695

这意味着排除对 ViewTreeObserver 的任何使用.

And that means any use of ViewTreeObserver is excluded.

有什么想法吗?

推荐答案

我建议使用我在链接答案中使用的相同近似策略:在创建 ViewHolder 时设置最大高度.但是,通用的 View 不支持 maxHeight 属性,因此我们将利用 ConstraintLayout 来实现我们想要的.

I'd recommend the same approximate strategy I used in my linked answer: set the max height when you create the ViewHolder. However, generic View does not support the maxHeight property, so we'll leverage ConstraintLayout to achieve what we want.

如果您的项目视图的根视图已经是一个 ConstraintLayout,那太好了,否则您可以将您所做的任何内容包装在其中.在这个人为的例子中,我使用了这个布局(FrameLayout 在 XML 中使用 0dp 宽度和 wrap_content 高度):

If the root view for your item views is already a ConstraintLayout, great, otherwise you can wrap whatever you do have inside it. In this contrived example, I'm using this layout (the FrameLayout is using 0dp width and wrap_content height in the XML):

<androidx.constraintlayout.widget.ConstraintLayout ...>
    <FrameLayout ...>
        <TextView .../>
    </FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

在创建时,我将 FrameLayout 的最大高度设置为父级高度的 60%:

At creation time, I set the max height of the FrameLayout to be 60% of the parent's height:

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
    val inflater = LayoutInflater.from(parent.context)
    val itemView = inflater.inflate(R.layout.recycler_item, parent, false)
    val holder = MyViewHolder(itemView)

    val params = holder.frame.layoutParams as ConstraintLayout.LayoutParams
    params.matchConstraintMaxHeight = (parent.height * 0.6).toInt()
    holder.frame.layoutParams = params

    return holder
}

这篇关于如何设置 RecyclerView 项目相对于父项的最大高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆