在RecyclerView ItemDecoration.getItemOffsets中获取子视图高度

时间:2017-09-06 21:35:52

标签: android android-recyclerview item-decoration

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

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

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

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

ItemDecoration:

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>

2 个答案:

答案 0 :(得分:2)

我发现这个解决方法适用于固定大小的子视图。我仍然不确定如何处理动态大小的布局。

// 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;

答案 1 :(得分:2)

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

在基于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)
}