Make last Item / ViewHolder in RecyclerView fill rest of the screen or have a min height

你。 提交于 2019-12-03 11:41:58

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
                }
            }
        }

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).

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.

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).

I used muthuraj solution to solve a similar problem, I wanted the last item to be shown at the last normally if previous items fill up the height of the page or more height but in case the previous item doesn't fill up the height, I wanted last item to be shown on the bottom of the page.

When previous item + specialItem take all the place.
--------------
item
item
item
specialItem
--------------

When previous item + specialItem take more than the height
--------------
item
item
item
item
item
item
specialItem
--------------

When previous item + specialItem take less than the height
--------------



specialItem
--------------
or
--------------
item


specialItem
--------------

to archive this I use this code

val lastItemView = holder.itemView
            //TODO use a better option instead of waiting for 200ms
            Handler().postDelayed({
                val lastItemTop = lastItemView.top
                val remainingSpace = recyclerViewHeight() - lastItemTop
                val heightToSet = Math.max(remainingSpace, minHeight)

                if (lastItemView.height != heightToSet) {
                    val layoutParams = lastItemView.layoutParams
                    layoutParams.height = heightToSet
                    lastItemView.layoutParams = layoutParams
                }
            }, 200)

the reason I use Handler().postDelayed is that doOnLayout never gets called for me and I couldn't figure out why so instead run the code with 200ms delay until I found something better to work with.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!