Android : How to selectively decorate RecyclerView items

on Friday, January 30, 2015


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?


0 comments:

Post a Comment