问题描述
我正在根据以下要点创建ItemDecoration
的子类: https://gist.github.com/alexfu/0f464fc3742f134ccd1e
I am creating a subclass of ItemDecoration
from this gist: https://gist.github.com/alexfu/0f464fc3742f134ccd1e
如何使其仅装饰具有特定条件的物品?例如,仅装饰具有特定位置,ViewHolder类型等的项目.
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项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!