Android library providing simple way to control divider items (ItemDecoration) of RecyclerView

Overview

RecyclerView-FlexibleDivider

Android Arsenal License Download

Android library providing simple way to control divider items of RecyclerView

Simple Divider Complex Divider

Release Note

[Release Note] (https://github.com/yqritc/RecyclerView-FlexibleDivider/releases)

Gradle

repositories {
    jcenter()
}

dependencies {
    compile 'com.yqritc:recyclerview-flexibledivider:1.4.0'
}

Usage

The following is the simplest usage.
Drawing a divider drawable retrieved from android.R.attr.listDivider between each cell.

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).build());
ItemDecoration Usage
HorizontalDividerItemDecoration For layout manager having vertical orientation to draw horizontal divider
VerticalDividerItemDecoration For layout manager having horizontal orientation to draw vertical divider
*Please note that you can only set one of above item decorations at a time.

If you want to set color, size and margin values, you can specify as the followings.

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.addItemDecoration(
        new HorizontalDividerItemDecoration.Builder(this)
                .color(Color.RED)
                .sizeResId(R.dimen.divider)
                .marginResId(R.dimen.leftmargin, R.dimen.rightmargin)
                .build());

Instead of setting color and size, you can set paint object.

Paint paint = new Paint();
paint.setStrokeWidth(5);
paint.setColor(Color.BLUE);
paint.setAntiAlias(true);
paint.setPathEffect(new DashPathEffect(new float[]{25.0f, 25.0f}, 0));
recyclerView.addItemDecoration(
        new HorizontalDividerItemDecoration.Builder(this).paint(paint).build());

Also 9patch drawable can be used for drawing divider.

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this)
        .drawable(R.drawable.sample)
        .size(15)
        .build());

If you want to customize divider depending on the position, implement the following interfaces.

List of provider

The following providers can be implemented and controllable for each divider drawn between cells.
Please refer to ComplexAdapter class in the sample for the usage of providers in detail.

  • ColorProvider Provide color for divider

  • PaintProvider Provide paint object for divider line to draw.

  • DrawableDivider Provide drawable object for divider line

  • SizeProvider Provide height for horizontal divider, width for vertical divider.

  • VisibilityProvider
    Enables you to control the visibility of dividers.

  • MarginProvider for horizontal divider (vertical list)
    Enables you to specify left and right margin of divider.

  • MarginProvider for vertical divider (horizontal list)
    Enables you to specify top and bottom margin of divider.

For GridLayoutManager, the position parameter of above providers is group index of items. So, control your divider based on [group index](http://developer.android.com/intl/ja/reference/android/support/v7/widget/GridLayoutManager.SpanSizeLookup.html#getSpanGroupIndex(int, int)) instead of the position of items.

Optional

  • Builder.showLastDivider
    Draw divider line at the end of last item in RecyclerView. If you enable this, the range of position parameter of providers listed above is 0 to itemCount-1. Otherwise, the range is 0 to itemCount-2.

  • Builder.positionInsideItem
    Draw divider inside items.
    If you want to follow material design guideline, enable this feature.

Note

  • When neither of color, paint, drawable is set, default divider retrieved from android.R.attr.listDivider will be used.
  • When you set Paint, you must use setColor and setStrokeWidth methods of paint class.
  • If you want to use DashPathEffect, please note the following issue. https://code.google.com/p/android/issues/detail?id=29944
Looking for custom ItemDecoration to achieve equal column space for GridLayoutManager?

Check out https://gist.github.com/yqritc/ccca77dc42f2364777e1

License

Copyright 2016 yqritc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Comments
  • Improves VerticalDividerItemDecoration when used in a GridLayout

    Improves VerticalDividerItemDecoration when used in a GridLayout

    The VerticalDividerItemDecoration was being drawn over the entire recyclerview, rather than just the cell bounds for each position.

    This meant that shouldHideDivider() had no effect.

    The bounds calcutation for the VerticalDividerItemDecoration is now correct.

    ShowLastDivider behaviour is also now supported for VerticalDividerItemDecoration in a GridLayout

    opened by josh-burton 8
  • draw divider on top of item instead of below

    draw divider on top of item instead of below

    From the Material Design Spec

    Dividers are placed along the bottom edge of the content tiles, independent of the grid.

    The current implementation draws the dividers below the item:

            if (mDividerType == DividerType.DRAWABLE) {
    -            bounds.top = child.getBottom() + params.topMargin + transitionY;
    +            // I haven't tested the drawable implementation
    +            bounds.top = child.getBottom() + params.topMargin + transitionY - dividerSize;
                bounds.bottom = bounds.top + dividerSize;
            } else {
    -            bounds.top = child.getBottom() + params.topMargin + dividerSize / 2 + transitionY;
    +            // this one works great
    +            bounds.top = child.getBottom() + params.topMargin - dividerSize / 2 + transitionY;
                bounds.bottom = bounds.top;
            }
    

    and this can be removed because the size does not change

        @Override
        protected void setItemOffsets(Rect outRect, int position, RecyclerView parent) {
    -        outRect.set(0, 0, 0, getDividerSize(position, parent));
    +        outRect.set(0, 0, 0, 0);
        }
    

    This feature should be optional

    opened by passsy 5
  • No divider drawn using ItemAnimator

    No divider drawn using ItemAnimator

    Hi,

    I'm using an ItemAnimator with an Horizontal LinearLayoutManager. When I call notifyItemRemoved or notifyItemInserted the drawers are not drawn until I scroll to new items. It seems related to this issue https://code.google.com/p/android/issues/detail?id=100653

    Could it be possible to fix it?

    Thanks !

    opened by JulienDev 4
  • Change the divider size depending not only on the position but also on the content?

    Change the divider size depending not only on the position but also on the content?

    Hi there,

    Is there a way of changing the divider size depending not only on the position but also on the content? In my case, my adapter extends from a CursorRecyclerAdapter and depending on the position content I set a different view type.

        @Override
        public int getItemViewType(int position) {
            if (mDataValid && mCursor != null && mCursor.moveToPosition(position)) {
                String cursorUid = mCursor.getString(mCursor.getColumnIndex(ChatProvider.COL_FROM_UID));
                String previousUid = "";
                if (!mCursor.isFirst()) {
                    mCursor.moveToPrevious();
                    previousUid = mCursor.getString(mCursor.getColumnIndex(ChatProvider.COL_FROM_UID));
                    mCursor.moveToNext();
                }
    
                if (cursorUid.equals(uid))
                    return VIEW_TYPE_ME;
                else if (previousUid.equals(cursorUid))
                    return VIEW_TYPE_OTHERS_BIS;
                else
                    return VIEW_TYPE_OTHERS;
    
            }
            return 0;
        }
    

    My problem is that I set the adapter to the recyclerview when the cursor is loaded inside the onLoadFinished method of the CursorLoader and the "dividerSize" method seems to get called before the cursor is loaded setting the default divider to all items.

    @Override
        public int dividerSize(int position, RecyclerView parent) {
            switch (getItemViewType(position)) {
                case VIEW_TYPE_ME:
                    Utils.convertDpToPixel(context.getResources().getDimension(R.dimen.divider_chat_normal), context);
                    break;
    
                case VIEW_TYPE_OTHERS:
                    Utils.convertDpToPixel(context.getResources().getDimension(R.dimen.divider_chat_normal), context);
                    break;
    
                case VIEW_TYPE_OTHERS_BIS:
                    Utils.convertDpToPixel(context.getResources().getDimension(R.dimen.divider_chat_bis), context);
                    break;
            }
            return Utils.convertDpToPixel(context.getResources().getDimension(R.dimen.divider_chat_normal), context);
        }
    

    I've tried to call addItemDecoration after the data is loaded into the cursor with no luck.

       @Override
        public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
            messagesChatAdapter.swapCursor(data);
            recyclerView.scrollToPosition(messagesChatAdapter.getItemCount() - 1);
            recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity())
                    .sizeProvider(messagesChatAdapter)
                    .color(android.R.color.transparent)
                    .build());
        }
    
    opened by MrBrightside29 4
  • why the divider color is not the same as that it should be?

    why the divider color is not the same as that it should be?

    Hi, I use the API as the doc says:

    recyclerView.addItemDecoration( new HorizontalDividerItemDecoration.Builder(this) .color(Color.RED) .sizeResId(R.dimen.divider) .marginResId(R.dimen.leftmargin, R.dimen.rightmargin) .build());

    However, I find that the color that shows is not the same as that it should be. Could you please help me out?

    opened by jhwsx 3
  • Move grid layout specific code to its own HorizontalGridDividerItemDe…

    Move grid layout specific code to its own HorizontalGridDividerItemDe…

    …coration

    Extend HorizontalDividerItemDecoration as it basically is the same except for calculating how many children to draw and not to redraw for children that already haven been drawn.

    opened by tobibo 3
  • Flashing divider when notifying multiple items changed

    Flashing divider when notifying multiple items changed

    I'm using radio buttons in my cells in a horizontal linear layout.

    I noticed that when calling notifyItemChanged on two cells at the same time, one to uncheck the radio button and one to check the radio button. That the dividers flash and glitch weirdly.

    The above can be worked around by calling notifyDataSetChanged(). But the same problem occurs when calling notifyItemRemoved and notifyItemChanged at the same time which I haven't been able to work around whilst still getting the removed animation.

    Thanks, Tom

    Example project https://github.com/Sylapse/DividerSample.git

    opened by tom-pratt 3
  • shouldHideDivider not working

    shouldHideDivider not working

    when set to true, divider is still showing

    I can confirm that my adapter is implementing FlexibleDividerDecoration.VisibilityProvider and overriding shouldHideDivider. I can also confirm that shouldHideDivider is not being called

    opened by PSesto 2
  • Added recycleview's reverse functionality

    Added recycleview's reverse functionality

    Changelog:

    • ItemDecorator can handle reversed recycleviews
    • More complex sample where you can change between many recycleview configuration without another activities
    opened by larten 2
  • change logic for handle show last divider

    change logic for handle show last divider

    1 skip draw divider only for the real last view when mShowLastDivider set to false 2 getItemOffsets return zero size Rect for last view when mShowLastDivider set to false

    opened by BennyWang 1
  • Memory Growing Issue

    Memory Growing Issue

    just install sample project. Run on an emulator. Open Memory Monitor. Scroll up and down with several times. Memory is growing. Sometimes GC start, but cannot release more memory. Always call GC.

    Any idea on this issue?

    Thanks

    opened by KingWu 1
  • jcenter() closed

    jcenter() closed

    recyclerview-flexibledivider package was hosted on jcenter() but it is closed now.

    Can you put it in another repository and can the change the read.me file

    opened by manking109 3
  • migrate to androidx dependencies

    migrate to androidx dependencies

    hi, thanks in advance for your library. recently google released the new androix libraries (AKA support libraries) i will be great if you update the lib dependencies to this news libraries

    opened by yesidlazaro 1
  • the divider show in left and right  even if  set margin

    the divider show in left and right even if set margin

    image

    
    HorizontalDividerItemDecoration mItemDecoration = new HorizontalDividerItemDecoration.Builder(this)
                    .color(ContextCompat.getColor(mContext, R.color.color_333333))
                    .sizeResId(R.dimen.px_1)
                    .marginResId(R.dimen.dp_16, R.dimen.dp_16)
                    .build();
    mRecyclerView.addItemDecoration(mItemDecoration);
    
    
    opened by Sum41forever 2
Releases(1.4.0)
Owner
Yoshihito Ikeda
Yoshihito Ikeda
*** WARNING: This library is no longer maintained *** An easy way to add a simple 'swipe-and-do-something' behavior to your `RecyclerView` items. Just like in Gmail or Inbox apps.

SwipeToAction An easy way to add a simple 'swipe-and-do-something' behavior to your RecyclerView items. Just like in Gmail or Inbox apps. Integration

Victor Calvello 223 Nov 16, 2022
Android ListView that mimics a GridView with asymmetric items. Supports items with row span and column span

AsymmetricGridView An Android custom ListView that implements multiple columns and variable sized elements. Please note that this is currently in a pr

Felipe Lima 1.8k Jan 7, 2023
Kotlin way of building RecyclerView Adapter 🧩. You do not have to write RecyclerView Adapters again and again and suffer from handling of different view types. Kiel will help you.

Kiel Kiel is a RecyclerView.Adapter with a minimalistic and convenient Kotlin DSL which provides utility on top of Android's normal RecyclerView.Adapt

ibrahim yilmaz 370 Jan 2, 2023
An Android Animation library which easily add itemanimator to RecyclerView items.

RecyclerView Animators RecyclerView Animators is an Android library that allows developers to easily create RecyclerView with animations. Please feel

Daichi Furiya 11.2k Jan 5, 2023
. Android library that integrate sticky section headers in your RecyclerView

recyclerview-stickyheaders Recyclerview-stickyheaders is an Android library that makes it easy to integrate section headers in your RecyclerView. Thes

null 968 Nov 10, 2022
Android library defining adapter classes of RecyclerView to manage multiple view types

RecyclerView-MultipleViewTypeAdapter RecyclerView adapter classes for managing multiple view types Release Note [Release Note] (https://github.com/yqr

Yoshihito Ikeda 414 Nov 21, 2022
Android library to achieve in an easy way, the behaviour of the home page in the Expedia app, with a pair of auto-scroll circular parallax ListViews.

ListBuddies This library is not maintained anymore and there will be no further releases Android library of a pair of auto-scroll circular parallax Li

JPARDOGO 970 Dec 29, 2022
RecyclerView extension library which provides advanced features. (ex. Google's Inbox app like swiping, Play Music app like drag and drop sorting)

Advanced RecyclerView This RecyclerView extension library provides Google's Inbox app like swiping, Play Music app like drag-and-drop sorting and expa

Haruki Hasegawa 5.2k Dec 23, 2022
[UNMAINTAINED] Sticky Headers decorator for Android's RecyclerView

This project is no longer being maintained sticky-headers-recyclerview This decorator allows you to easily create section headers for RecyclerViews us

timehop 3.7k Dec 31, 2022
Pumped up RecyclerView

##Description This is an attempt to make RecyclerView easier to use. Features built in: ProgressBar while adapter hasn't been set EmptyView if adapter

Anton Malinskiy 2.6k Jan 5, 2023
A very easy-to-use and non-intrusive implement of Swipe to dismiss for RecyclerView.

RecyclerViewSwipeDismiss A very easy-to-use and non-intrusive implement of Swipe to dismiss for RecyclerView. Preview How to use Add these lines to yo

xcodebuild 431 Nov 23, 2022
An adapter which could be used to achieve a parallax effect on RecyclerView.

android-parallax-recycleview Integration Step 1. Add the JitPack repository to your build file repositories { maven { url "https://jitpack

Pedro Oliveira 1.6k Nov 17, 2022
A Fast Scroller for the RecyclerView world!

RecyclerViewFastScroller The RecyclerViewFastScroller is a widget that can be added to a layout and connected to a RecyclerView for fast scrolling. Th

Daniel Smith 1.1k Dec 19, 2022
Dividers is a simple Android library to create easy separators for your RecyclerViews

Dividers Dividers is an Android library to easily create separators for your RecyclerViews. It supports a wide range of dividers from simple ones, tha

Karumi 490 Dec 28, 2022
Android library to display a ListView whose cells are not rigid but flabby and react to ListView scroll.

FlabbyListView This library is not maintained anymore and there will be no further releases Android library to display a ListView which cells are not

JPARDOGO 762 Nov 23, 2022
An android library for section headers that stick to the top

StickyListHeaders StickyListHeaders is an Android library that makes it easy to integrate section headers in your ListView. These section headers stic

Emil Sjölander 5.5k Jan 2, 2023
Android library to observe scroll events on scrollable views.

Android-ObservableScrollView Android library to observe scroll events on scrollable views. It's easy to interact with the Toolbar introduced in Androi

Soichiro Kashima 9.6k Dec 29, 2022
An Android custom ListView and ScrollView with pull to zoom-in.

PullZoomView An Android custom ListView and ScrollView with pull to zoom-in. Features Set ZoomView enable Add HeaderView Custom ZoomView Parallax or N

Frank-Zhu 2.3k Dec 26, 2022
Drag and drop GridView for Android

DynamicGrid Drag and drop GridView for Android. Depricated It's much better to use solutions based on recycler view. For example https://github.com/h6

Alex Askerov 920 Dec 2, 2022