Giter Club home page Giter Club logo

simple-item-decoration's Introduction

Simple Item Decoration

A library for adding dividers and offsets to Android's RecyclerView using RecyclerView.ItemDecoration.

For more information please see the website.

Download

v1.0.0 AAR

Gradle

compile 'com.bignerdranch.android:simple-item-decoration:1.0.0'

Maven

<dependency>
    <groupId>com.bignerdranch.android</groupId>
    <artifactId>simple-item-decoration</artifactId>
    <version>1.0.0</version>
</dependency>

License

The MIT License

Copyright (c) 2015 Big Nerd Ranch

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

simple-item-decoration's People

Contributors

cemore2048 avatar dgreenhalgh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

simple-item-decoration's Issues

"Offset" vs. "Divider" API inconsistency

StartOffsetItemDecoration and EndOffsetItemDecoration's constructors take in an integer value for the size of an offset in pixels. DividerItemDecoration's constructor takes a Drawable for a divider.

All of these ItemDecorations should feature constructors for each a Drawable divider and an integer pixel offset.

GridDividerItemDecoration crashes on an empty list

GridDividerItemDecoration.drawHorizontalDividers() assumes that the number of items in the list is at least the number of columns in the grid (see View child = parent.getChildAt(i);)

private void drawHorizontalDividers(Canvas canvas, RecyclerView parent) {
    int parentTop = parent.getPaddingTop();
    int parentBottom = parent.getHeight() - parent.getPaddingBottom();

    for (int i = 0; i < mNumColumns; i++) {
        View child = parent.getChildAt(i);
        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

        int parentLeft = child.getRight() + params.rightMargin;
        int parentRight = parentLeft + mHorizontalDivider.getIntrinsicWidth();

        mHorizontalDivider.setBounds(parentLeft, parentTop, parentRight, parentBottom);
        mHorizontalDivider.draw(canvas);
    }
}

There needs to be a null check before the next line that assumes child is not null.

Handle null Drawables

The user may want to pass a null Drawable to GridDividerItemDecoration in the case that they want to only add a horizontal or vertical divider. We should handle this case.

Manifest merger failed : Attribute application@label

When I run ./gradlew assembleRelease on my application which includes com.bignerdranch.android:simple-item-decoration:1.0.0, I get the following error:

AndroidManifest.xml:23:9-36 Error:
        Attribute application@label value=(AirportList) from AndroidManifest.xml:23:9-36
        is also present at [com.bignerdranch.android:simple-item-decoration:1.0.0] AndroidManifest.xml:13:9-41 value=(@string/app_name).
        Suggestion: add 'tools:replace="android:label"' to <application> element at AndroidManifest.xml:21:5-46:19 to override.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:processReleaseManifest'.
> Manifest merger failed : Attribute application@label value=(AirportList) from AndroidManifest.xml:23:9-36
        is also present at [com.bignerdranch.android:simple-item-decoration:1.0.0] AndroidManifest.xml:13:9-41 value=(@string/app_name).
        Suggestion: add 'tools:replace="android:label"' to <application> element at AndroidManifest.xml:21:5-46:19 to override.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 9s

For now I didn't try the workaround because I decided to implement the feature in a way that doesn't depend on simple-item-decoration, but I thought I should report the problem here in case you want to fix it. I guess app_name just needs to be removed from the AndroidManifest.xml.

Only draw item dividers that are visible

Current implementation iterates over all items to draw dividers, this should be limited to only the items that are on the screen, except you may have to expand to 1 item outside the items that are completely visible.

Avoid separate implementations for each LayoutManager

In order for this library to be simple, we shouldn't make the user use a separate API if they are using a different LayoutManager. Let's get the LayoutManager from the parent RecyclerView in ItemDecoration.getItemOffsets and switch behavior based on it. This will allow us to fold the following implementations together:

  • DividerItemDecoration <- GridDividerItemDecoration
  • StartOffsetItemDecoration <- GridTopOffsetItemDecoration
  • EndOffsetItemDecoration <- GridBottomOffsetItemDecoration

Moving forward with just three ItemDecorations will enhance the usefulness of Simple Item Decoration as a library, rather than a collection of samples.

Support reverseLayout

We currently have no logic to handle the case in which a user reverses the layout of their LayoutManager. We should handle this case and reverse our divider addition accordingly.

GridDividerItemDecoration item offsets do not space columns correctly

The current implementation of GridDividerItemDecoration that works for vertically scrolling lists does not space the columns out correctly. This can be seen when using dividers that are similar in size to the column widths.

This is because each column gets further offset to the right and the last one runs out of space.

The issue happens in GridDividerItemDecoration.getItemOffsets(), and a fix is hopefullt to replace with the following code:

public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    super.getItemOffsets(outRect, view, parent, state);

    int childAdapterPosition = parent.getChildAdapterPosition(view);

    // proportion the offsets correctly given a fixed width
    if (mNumColumns > 1) {
        int columnPosition = childAdapterPosition % mNumColumns;
        int nudge = Math.round((float) mHorizontalDivider.getIntrinsicWidth() / mNumColumns);

        outRect.left = columnPosition * nudge;
        outRect.right = (mNumColumns - columnPosition - 1) * nudge;
    }

    // vertical offset is constant
    boolean childIsInFirstRow = childAdapterPosition < mNumColumns;
    if (!childIsInFirstRow) {
        outRect.top = mVerticalDivider.getIntrinsicHeight();
    }
}

I am struggling to get the master branch to run properly due to #31 and #33 so cannot create a PR at this point in time, but will try to find a way around those and submit a fix correctly.

Simplify sample app

The sample app has grown into a spaghetti mess of duplicated code and callbacks. This misses the point of a sample app by adding unnecessary complexity.

Get rid of the lib's Android Manifest

AndroidManifest.xml shouldn't be needed. Also it's causing unnecessary manifest merge conflicts w/ my app.

Error:Execution failed for task ':app:processXXXXDebugManifest'.
> Manifest merger failed : Attribute application@allowBackup value=(false) from AndroidManifest.xml:28:9-36
    is also present at [com.bignerdranch.android:simple-item-decoration:1.0.0] AndroidManifest.xml:12:9-35 value=(true).
    Suggestion: add 'tools:replace="android:allowBackup"' to <application> element at AndroidManifest.xml:26:5-96:19 to override.

App crash when data count is less than columns count in GridDividerItemDecoration

App crash when data count is less than columns count (if I'm using GridDividerItemDecoration)
Below log:

08-22 06:45:15.364 5852-5852/pl.myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: pl.myapp, PID: 5852
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference
at com.dgreenhalgh.android.simpleitemdecoration.grid.GridDividerItemDecoration.drawHorizontalDividers(GridDividerItemDecoration.java:84)
at com.dgreenhalgh.android.simpleitemdecoration.grid.GridDividerItemDecoration.onDraw(GridDividerItemDecoration.java:43)
at android.support.v7.widget.RecyclerView.onDraw(RecyclerView.java:3658)
at android.view.View.draw(View.java:15231)
at android.support.v7.widget.RecyclerView.draw(RecyclerView.java:3593)
at android.view.View.updateDisplayListIfDirty(View.java:14167)
at android.view.View.getDisplayList(View.java:14189)
at android.view.View.draw(View.java:14959)
at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.updateDisplayListIfDirty(View.java:14162)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14127)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14127)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14127)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14127)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14127)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14127)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14127)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14127)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:273)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:279)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:318)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2530)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2352)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1982)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.h

Maven Dependency

Is this library available as a maven dependency? Can I plop it in my gradle file?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.