andrew
andrew

Reputation: 3899

RecyclerView in RecyclerView: ItemDecoration

I'm using a recyclerview (with a vertical LinearLayout) with different viewholders, one of them has another RecyclerView (with an horizontal LL). In this second recyclerView I'm using an ItemDecoration to add a left margin to all of its items but the first.

The code in onBindViewHolder of the first RecyclerView related to my problem is:

RecyclerView mRecyclerView = holder.mRecyclerView;
mRecyclerView.addItemDecoration(new SpaceLeftItemDecoration(5));
mRecyclerView.setAdapter(new MyAdapter(mDataSet, context));
mRecyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));

Everytime this code is executed, a new ItemDecorations is added to the recyclerview. If I scroll up and down 3 or 4 times, the spacing between items in the second recyclerview is really noticeable.

So my question is, how can I know if the RecyclerView already has an ItemDecoration?

Thanks.

Upvotes: 1

Views: 840

Answers (1)

florent champigny
florent champigny

Reputation: 979

Yes, move this code to createViewHolder, so it will be called only once.

Upvotes: 2

Related Questions