如何有选择地装饰 RecyclerView 项目 [英] How to selectively decorate RecyclerView items

查看:18
本文介绍了如何有选择地装饰 RecyclerView 项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在根据以下要点创建 ItemDecoration 的子类:https://gist.github.com/alexfu/0f464fc3742f134ccd1e

I am creating a subclass of ItemDecoration from this gist: https://gist.github.com/alexfu/0f464fc3742f134ccd1e

如何让它只装饰特定条件的物品?例如,只装饰具有特定位置、ViewHolder 类型等的 item.

How to make it only decorate items with certain condition? For instance, only decorate items with certain positions, type of ViewHolder, etc.

我已经使用此代码修改了上述要点(以及对已弃用的 Android API 进行了一些更改),但是所有项目都得到了装饰:

I have modified the above mentioned gist (plus some changes on deprecated Android API) with this code, but all items get decorated anyway:

public boolean isDecorated(View view, RecyclerView parent) {
    RecyclerView.ViewHolder holder = parent.getChildViewHolder(view);
    return holder instanceof MenuIconViewHolder || holder instanceof MenuDetailViewHolder;
}

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    if (isDecorated(view, parent)) {
        if (mOrientation == VERTICAL_LIST) {
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
        } else {
            outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
        }
    } else {
        super.getItemOffsets(outRect, view, parent, state);
    }
}

上面的代码有什么问题?顺便说一句,将这种代码放在 ItemDecoration 类中是否可以被视为最佳实践(就关注点分离而言)?

What's wrong with above code? By the way, can it be considered best practice (in respect of separation of concerns) to place that kind of code in ItemDecoration class?

推荐答案

您还需要在 draw 方法上调用 isDecorated,因为目前您没有在这些项目上放置偏移量,但您仍然在其上进行绘制.

You need to call isDecorated on the draw method as well, because at the moment you don't put the offset on those items but you still draw over it.

该方法循环当前在屏幕上可见的 RecyclerView 中的所有子视图.

The method loops over all the child views currently in the RecyclerView visible on the screen.

public void drawVertical(Canvas c, RecyclerView parent) {
    final int left = parent.getPaddingLeft();
    final int right = parent.getWidth() - parent.getPaddingRight();

    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        if(isDecorated(child, parent))
        {
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                .getLayoutParams();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }
}

public void drawHorizontal(Canvas c, RecyclerView parent) {
    final int top = parent.getPaddingTop();
    final int bottom = parent.getHeight() - parent.getPaddingBottom();

    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        if(isDecorated(child, parent))
        {
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                .getLayoutParams();
            final int left = child.getRight() + params.rightMargin;
            final int right = left + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }
}

这篇关于如何有选择地装饰 RecyclerView 项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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