I am creating a subclass of ItemDecoration
from this gist: https://gist.github.com/alexfu/0f464fc3742f134ccd1e
How to make it only decorate items with certain condition? For instance, only decorate items with certain positions, type of ViewHolder, etc.
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);
}
}
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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…