为什么RecyclerView onDraw延迟

时间:2017-01-14 04:07:28

标签: java android android-recyclerview

我想要itemDecoration只为前三个孩子绘制分隔符。所以,我在正确的时间返回onDraw。但是,我发现itemDecoration onDraw()延迟了。并且导致绘制分隔符动作延迟以回收视图滑动结束。

这是我的ItemDecoration onDraw()方法:

@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
    RecyclerView.Adapter adapter = parent.getAdapter();
    if (adapter == null) {
        return;
    }

    int itemCount = adapter.getItemCount();
    int lastDividerOffset = getLastDividerOffset(parent);
    int validChildCount = parent.getChildCount();
    int lastChildPosition = -1;
    for (int i = 0; i < validChildCount; i++) {
        //只给前三个child设置 divider
        View child = parent.getChildAt(i);
        int childAdapterPosition = parent.getChildAdapterPosition(child);
        if (childAdapterPosition >= 3) {
            return;
        }

getItemOffsets()方法:

@Override
public void getItemOffsets(Rect rect, View v, RecyclerView parent, RecyclerView.State state) {
    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        View child = parent.getChildAt(i);
        int childAdapterPosition = parent.getChildAdapterPosition(child);
        if (childAdapterPosition >= 3) {
            return;
        }
    }
    int position = parent.getChildAdapterPosition(v);
    int itemCount = parent.getAdapter().getItemCount();
    int lastDividerOffset = getLastDividerOffset(parent);
    if (!mShowLastDivider && position >= itemCount - lastDividerOffset) {
        // Don't set item offset for last line if mShowLastDivider = false
        return;
    }

    int groupIndex = getGroupIndex(position, parent);
    if (mVisibilityProvider.shouldHideDivider(groupIndex, parent)) {
        return;
    }

    setItemOffsets(rect, groupIndex, parent);
}

感谢。

1 个答案:

答案 0 :(得分:0)

某些xml的设置动画会导致此问题。