1

I need to add divider between two elements in my RecyclerView, but not between every two elements, only between one.

OK, I will explain you in better way. I have two groups of elements, and I need to make some kind of custom divider between them. enter image description here

How can I achieve this in the most optimized way. I have idea to change ItemLayout and add splitter, but I don't think that this option will be fast enough.

Community
  • 1
  • 1
Filip V
  • 435
  • 4
  • 16
  • Hi, do your Adapter already handles two types of items (red and blue in a picture)? – Michael Spitsin Aug 18 '16 at 12:44
  • Well, yes and no, I use multi type layouts but no in this way. For example I have two layouts Apple and Pear, red ones are fresh apples and pears and blue ones are eaten fruit. – Filip V Aug 18 '16 at 12:47

3 Answers3

0

My assumption is that you're probably already using different itemviewtypes for the blue and red items. I would recommend creating another itemviewtype for your divider, add a dummy object to your data list and check in the RecyclerView.Adapter.onCreateViewHolder if there is such a object at the current position with instanceof. Then return a viewholder with your desired divider layout.

A tutorial about viewtypes can be found here: How to create RecyclerView with multiple view type?.

Community
  • 1
  • 1
Jacob
  • 493
  • 6
  • 17
0

I think, using additional type of layout for singular splitter is a good choice. If you warning about performance, then I will calm you, there will no any performance issues with that:

  • you will have simple layout with some static text or simple drawable (grey line, for example). Creating of such layout will take few milliseconds or maybe less and binding it will took even less time
  • recycler view, as far as I know, have their own cache for each types of items, so you splitter will be (probably only once) created and then cached for future bindings
  • if you look to libraries, that provide ListViews with sections, then you will see, that they use this approach as well (I mean, that they will have items of default types and splitter-type-items or section-type-items)

Of course you can use ItemDecoration, but then you will need to handle all by your own. You will need to determine child position and decide, do you need to draw splitter or not. And it's more painful, I think.

Michael Spitsin
  • 2,539
  • 2
  • 19
  • 29
0

My solution here https://pastebin.com/4J7sPaCF :

public class SingleDividerDecoration extends DividerItemDecoration {

    private final int height;
    private int position = -1;

    public SingleDividerDecoration(Context context, @DrawableRes int dividerId) {
        super(context, LinearLayout.VERTICAL);
        Drawable divider = ContextCompat.getDrawable(context, dividerId);
        this.height = divider.getIntrinsicHeight();
        setDrawable(divider);
    }

    public void setPosition(int position) {
        this.position = position;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int viewPosition = parent.getChildAdapterPosition(view);
        int dividerHeight = viewPosition == position ? height : 0;
        outRect.set(0, 0, 0, dividerHeight);
    }
}
danik
  • 796
  • 9
  • 17