使RecyclerView中的最后一个Item / ViewHolder填充屏幕的其余部分或具有最小高度 [英] Make last Item / ViewHolder in RecyclerView fill rest of the screen or have a min height

查看:1040
本文介绍了使RecyclerView中的最后一个Item / ViewHolder填充屏幕的其余部分或具有最小高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使用RecyclerView。我使用回收者视图显示模型类的详细信息。

I'm struggeling with the RecyclerView. I use a recycler view to display the details of my model class.

//My model class
MyModel {
    String name;
    Double latitude;
    Double longitude;
    Boolean isOnline;
    ...
}

由于某些值可能不存在,我将RecyclerView与自定义视图类型一起使用(一个代表我模型的每个值)。

Since some of the values might not be present, I use the RecyclerView with custom view types (one representing each value of my model).

//Inside my custom adapter
public void setModel(T model) {
    //Reset values
    itemCount = 0;
    deviceOfflineViewPosition = -1;
    mapViewPosition = -1;

    //If device is offline, add device offline item
    if (device.isOnline() == null || !device.isOnline()) {
        deviceOfflineViewPosition = itemCount;
        itemCount++;
    }

    //Add additional items if necessary
    ...

    //Always add the map as the last item
    mapViewPosition = itemCount;
    itemCount++;

    notifyDataSetChanged();
}

@Override
public int getItemViewType(int position) {
    if (position == deviceOfflineViewPosition) {
        return ITEM_VIEW_TYPE_OFFLINE;
    } else if (position == mapViewPosition) {
        return ITEM_VIEW_TYPE_MAP;
    } else if (...) {
        //Check for other view types
    }
}

使用RecyclerView,我可以在运行时轻松确定可用的值,并将相应的项添加到RecyclerView数据源。我简化了代码,但是我的模型有更多的值,并且我有更多的视图类型。

With the RecyclerView I can easily determine at runtime which values are available and add corresponding items to the RecyclerView datasource. I simplyfied the code but my model has a lot more values and I have a lot more view types.

RecyclerView中的最后一项始终是地图,并且始终是当下。即使我的模型中根本没有价值,地图也将至少有一项。

The last item in the RecyclerView is always a map and it is always present. Even if there is no value at all in my model, there will at least be one item, the map.

问题:如何使RecyclerView中的最后一个项目充满屏幕上的剩余空间并保持最小高度。大小应为更大的值:剩余空间或最小高度。例如:

PROBLEM: How can I make the last item in RecyclerView fill the remaining space on screen and also have a min heigh. The size shall be what ever value is lager: the remaining space or the min height. For example:


  • 模型具有一些值,这些值合计占600dp屏幕的100dp->地图高度应为500dp

  • 模型有很多值,总计需要600dp屏幕的500dp-> map heigh的最小值应为200dp

  • 模型有没有值->地图填满整个屏幕

推荐答案

在布置完最后一项并将其添加到 minHeight 最后一项。

You can find the remaining space in RecyclerView after laying out the last item and add that remaining space to the minHeight of the last item.

val isLastItem = getItemCount() - 1 == position
if (isLastItem) {
            val lastItemView = holder.itemView
            lastItemView.doOnLayout {
                val recyclerViewHeight = recyclerView.height
                val lastItemBottom = lastItemView.bottom
                val heightDifference = recyclerViewHeight - lastItemBottom
                if (heightDifference > 0) {
                    lastItemView.minimumHeight = lastItemView.height + heightDifference
                }
            }
        }

onBindHolder 中,使用 getItemCount()-1 == position 。如果是最后一项,则通过在lastItem bottom( getBottom()给出相对于其父视图的视图的最底端像素)中减去recyclerView height来找到高度差。在这种情况下,我们的父级是 RecyclerView )。

In onBindHolder check if the item is last item using getItemCount() - 1 == position. If it is the last item, find the height difference by subtracting recyclerView height with lastItem bottom (getBottom() gives you the bottom most pixel of the view relative to it's parent. In this case, our parent is RecyclerView).

如果差异大于0,则将其添加到最后一个视图的当前高度,并将其设置为 minHeight 。我们将其设置为 minHeight ,而不是直接设置为 height 以支持最后一个视图的动态内容更改。

If the difference is greater than 0, then add that to the current height of the last view and set it as minHeight. We are setting this as minHeight instead of setting directly as height to support dynamic content change for the last view.

注意:这段代码是Kotlin,而doOnLayout函数来自 Android KTx 。同样,您的 RecyclerView 高度应为 match_parent 才能正常工作(我想这很明显)。

Note: This code is Kotlin, and doOnLayout function is from Android KTx. Also your RecyclerView height should be match_parent for this to work (I guess that's obvious).

这篇关于使RecyclerView中的最后一个Item / ViewHolder填充屏幕的其余部分或具有最小高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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