4

我正在尝试创建一个 ItemDecoration,它将通过动态添加填充到最后一个项目来保持 RecyclerView 的最小高度。

要计算填充量,我需要知道所有孩子的总高度。我通过获取单个视图的高度并将其乘以适配器中的项目数来近似此值。

我遇到的问题是 view.getHeight() 并不总是返回正确的高度。通常第一次调用 getItemOffsets() 时,高度远小于应有的高度(例如 30px 与 300px)。即使我在布局 XML 中给子视图一个固定的高度,也会发生这种情况。我猜这与测量/布局周期有关,因为我不确定如何在 ItemDecoration 中获得正确的视图尺寸。

在 getItemOffsets() 中以编程方式获取子视图高度的正确方法是什么?

物品装饰:

public class MinHeightPaddingItemDecoration extends RecyclerView.ItemDecoration {
    private static final String LOG_TAG = MinHeightPaddingItemDecoration.class.getSimpleName();

    private int mMinHeight;

    public MinHeightPaddingItemDecoration(int minHeight) {
        super();
        mMinHeight = minHeight;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView recyclerView, RecyclerView.State state) {
        int itemCount = state.getItemCount();
        int lastPosition = itemCount - 1;
        int itemPosition = recyclerView.getChildAdapterPosition(view);
        int layoutPosition = recyclerView.getChildLayoutPosition(view);

        // If this view isnt on screen then do nothing
        if (layoutPosition != RecyclerView.NO_POSITION && itemPosition == lastPosition) {

            // NOTE: view.getHeight() doesn't always return the correct height, even if the layout is given a fixed height in the XML
            int childHeight = view.getHeight();

            int totalChildHeight = childHeight * itemCount;

            int minHeight = getMinHeight();
            if (totalChildHeight < minHeight) {
                outRect.bottom = minHeight - totalChildHeight;
            }
        }
    }

    private int getMinHeight() {
        return mMinHeight;
    }
}

recycler_view_item.xml

<?xml version="1.0" encoding="utf-8"?>
<layout
    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">
    <data>
        <variable name="controller" type="my.ViewController"/>
    </data>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:clickable="true"
        android:onClick="@{() -> controller.doSomething()}">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Child layout"
                    />
        </RelativeLayout>

    </android.support.v7.widget.CardView>
</layout>
4

2 回答 2

6

我为固定大小的子视图找到了这个解决方法。我仍然不确定如何处理动态大小的布局。

// NOTE: view.getHeight() doesn't always return the correct height 
// NOTE: even if the layout is given a fixed height in the XML. 
// NOTE: Instead directly access the LayoutParams height value
int childHeight = view.getLayoutParams().height;
于 2017-09-06T21:42:11.043 回答
3

在阅读https://yoda.entelect.co.za/view/9627/how-to-android-recyclerview-item-decorations后,我发现了一种 hack 。

在根据 child.width 计算填充之前,如果需要,我会手动测量 View:

override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
    if(view.width == 0) fixLayoutSize(view, parent)
    calculatePadding(outRect, view, parent, state)
}

private fun fixLayoutSize(view: View, parent: ViewGroup) {
    if (view.layoutParams == null) {
        view.layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
        )
    }
    val widthSpec = View.MeasureSpec.makeMeasureSpec(parent.width, View.MeasureSpec.EXACTLY)
    val heightSpec = View.MeasureSpec.makeMeasureSpec(parent.height, View.MeasureSpec.UNSPECIFIED)
    val childWidth = ViewGroup.getChildMeasureSpec(
        widthSpec, parent.paddingLeft + parent.paddingRight, view.layoutParams.width
    )
    val childHeight = ViewGroup.getChildMeasureSpec(
        heightSpec, parent.paddingTop + parent.paddingBottom, view.layoutParams.height
    )
    view.measure(childWidth, childHeight)
    view.layout(0, 0, view.measuredWidth, view.measuredHeight)
}
于 2018-12-19T09:28:20.637 回答