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
ItemDecoration for RecyclerView using LinearLayoutManager for Android

RecyclerItemDecoration RecyclerItemDecoration allows you to draw divider between items in recyclerview with multiple ViewType without considering item

magiepooh 328 Dec 27, 2022
ItemDecoration for Android Recyclerview

RecyclerView ItemDecorations This project showcases some ItemDecorations for RecyclerViews. There is a blog post about the basics of drawing decoratio

David Medenjak 425 Nov 10, 2022
Pagination-RecyclerView - Simple and easy way to Paginating a RecyclerView

Pagination-RecyclerView Simple and easy way to Paginating a RecyclerView Android

Rakshit Nawani 0 Jan 3, 2022
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 8, 2023
Android library for RecyclerView to manage order of items and multiple view types.

recyclerview-binder Android Library for RecyclerView to manage order of items and multiple view types. Features Insert any items to wherever you want

Satoru Fujiwara 185 Nov 15, 2022
A Common RecyclerView.Adapter implementation which supports all kind of items and has useful data operating APIs such as remove,add,etc.

##PowerfulRecyclerViewAdapter A Common RecyclerView.Adapter implementation which supports any kind of items and has useful data operating APIs such as

null 313 Nov 12, 2022
ANDROID. ChipsLayoutManager (SpanLayoutManager, FlowLayoutManager). A custom layout manager for RecyclerView which mimicric TextView span behaviour, flow layouts behaviour with support of amazing recyclerView features

ChipsLayoutManager This is ChipsLayoutManager - custom Recycler View's LayoutManager which moves item to the next line when no space left on the curre

Oleg Beloy 3.2k Dec 25, 2022
TikTok-RecyclerView - This is a demo app built using 'Koin' a new dependency injection framework for Android along with RecyclerView and ExoPlayer2.

TikTok-RecyclerView Demo About This is a demo app built using 'Koin' a new dependency injection framework for Android along with RecyclerView and ExoP

Baljeet Singh 19 Dec 28, 2022
An adapter to create Android RecyclerViews with sections, providing headers and footers.

⚠ This library is no longer maintained ⚠️ SectionedRecyclerView An adapter to create Android RecyclerViews with sections, providing headers and footer

Tomás Ruiz-López 809 Dec 21, 2022
[] Super fast and easy way to create header for Android RecyclerView

DEPRECATED I created this library back in the day when I thought RecyclerView was all new and difficult. Writing an adapter that could inflate multipl

Bartek Lipinski 1.3k Dec 28, 2022
Square Cycler API allows you to easily configure an Android RecyclerView declaratively in a succinct way.

Square Cycler – a RecyclerView API The Square Cycler API allows you to easily configure an Android RecyclerView declaratively in a succinct way. Desig

Square 791 Dec 23, 2022
A RecyclerView that implements pullrefresh and loadingmore featrues.you can use it like a standard RecyclerView

XRecyclerView a RecyclerView that implements pullrefresh , loadingmore and header featrues.you can use it like a standard RecyclerView. you don't need

XRecyclerView 5.3k Dec 26, 2022
A RecyclerView that implements pullrefresh and loadingmore featrues.you can use it like a standard RecyclerView

XRecyclerView a RecyclerView that implements pullrefresh , loadingmore and header featrues.you can use it like a standard RecyclerView. you don't need

XRecyclerView 5.3k Dec 26, 2022
Carousel Recyclerview let's you create carousel layout with the power of recyclerview by creating custom layout manager.

Carousel Recyclerview let's you create carousel layout with the power of recyclerview by creating custom layout manager.

Jack and phantom 504 Dec 25, 2022
RecyclerView : SleepQualityTracker with RecyclerView app

RecyclerView - SleepQualityTracker with RecyclerView app SleepQualityTracker with RecyclerView This app builds on the SleepQualityTracker developed pr

Kevin 2 May 14, 2022
RecyclerView with DiffUtil is a way to improve the performance of your app

RecylerViewSamples RecyclerView with DiffUtil is a way to improve the performanc

Chhote Lal Pal 0 Dec 20, 2021
Examples of custom recycler view items. Automatically detecting a dominant color of an image using Picasso and Palette libraries

custom-image-list-item Examples of custom RecyclerView items using Data Binding Features: Loading images urls with the help of a Picasso library Autom

Alina Stepanova 2 Sep 12, 2022
[] RecyclerView made simple

TwoWayView RecyclerView made simple. Features A LayoutManager base class that greatly simplifies the development of custom layouts for RecyclerView A

Lucas Rocha 5.3k Dec 30, 2022