Skip to content

Commit

Permalink
show or invisible top item divider
Browse files Browse the repository at this point in the history
  • Loading branch information
donniesky committed Jun 5, 2017
1 parent 3ee0e77 commit 3b8cc1a
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 14 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ android {
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "me.donnie.divider"
minSdkVersion 14
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
Expand All @@ -28,4 +28,5 @@ dependencies {
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile project(':library')
compile 'com.jaeger.recyclerviewdivider:library:1.0.1'
}
11 changes: 4 additions & 7 deletions app/src/main/java/me/donnie/divider/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.donnie.divider;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
Expand All @@ -20,14 +19,12 @@ protected void onCreate(Bundle savedInstanceState) {

rv = (RecyclerView) findViewById(R.id.rv);

new Divider.Builder(this)
.hideLastDivider()
.drawable(ContextCompat.getDrawable(this, R.drawable.dash_divider))
.build().addTo(rv);


rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

new Divider.Builder(this)
.showTopDivider()
.size(80)
.build().addTo(rv);
rv.setAdapter(new TestAdaper(getData()));
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rv"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="me.donnie.divider.MainActivity"/>
2 changes: 2 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>

<item name="android:listDivider">@drawable/divider</item>
</style>

</resources>
38 changes: 33 additions & 5 deletions library/src/main/kotlin/me/donnie/divider/Divider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Divider constructor(@param:Type val type: Long,
private var size: Int? = null
private var marginSize: Int? = null
private var hideLastDivider: Boolean = false
private var showTopDivider: Boolean = false

private var visibleFactory: VisibleFactory? = null
private var drawableFactory: DrawableFactory? = null
Expand Down Expand Up @@ -106,6 +107,11 @@ class Divider constructor(@param:Type val type: Long,
return this
}

fun showTopDivider(): Builder {
this.showTopDivider = true
return this
}

fun visibleFactory(visibleFactory: VisibleFactory?): Builder {
this.visibleFactory = visibleFactory
return this
Expand Down Expand Up @@ -135,6 +141,8 @@ class Divider constructor(@param:Type val type: Long,
if (visibleFactory == null) {
if (hideLastDivider) {
visibleFactory = VisibleFactory.getLastItemInvisibleFactory()
} else if (showTopDivider) {
visibleFactory = VisibleFactory.getTopItemVisibleFactory()
} else {
visibleFactory = VisibleFactory.getDefault()
}
Expand All @@ -150,7 +158,7 @@ class Divider constructor(@param:Type val type: Long,

if (drawableFactory == null) {
var currentDrawable: Drawable? = null
when(this.type) {
when (this.type) {
TYPE_COLOR -> {
if (this.color != null) {
currentDrawable = color!!.colorToDrawable()
Expand Down Expand Up @@ -212,7 +220,7 @@ class Divider constructor(@param:Type val type: Long,
val orientation = parent.getOrientation()
val spanCount = parent.getSpanCount()
val childCount = parent.childCount
for (i in 0..childCount-1) {
for (i in 0..childCount - 1) {
val child = parent.getChildAt(i)
val itemPosition = parent.getChildAdapterPosition(child)
val groupIndex = parent.getGroupIndex(itemPosition)
Expand Down Expand Up @@ -302,13 +310,18 @@ class Divider constructor(@param:Type val type: Long,
}
}
}

top = childBottom + params.bottomMargin
bottom = top + size
left = childLeft + margin - marginToAddBefore
right = childRight - margin + marginToAddAfter

setBoundsAndDraw(divider, c, left, top, right, bottom)

if (childCount > 0 && showDivider == VisibleFactory.SHOW_TOP) {
bottom = childTop + params.topMargin
top = bottom - size
setBoundsAndDraw(divider, c, left, top, right, bottom)
}
} else {
if (spanCount > 1 && spanSize < spanCount) {
left = childLeft + margin
Expand Down Expand Up @@ -362,6 +375,13 @@ class Divider constructor(@param:Type val type: Long,
right = left + size

setBoundsAndDraw(divider, c, left, top, right, bottom)

if (childCount > 0 && showDivider == VisibleFactory.SHOW_TOP) {
right = childLeft + params.leftMargin
left = right - size

setBoundsAndDraw(divider, c, left, top, right, bottom)
}
}
}
}
Expand Down Expand Up @@ -402,7 +422,11 @@ class Divider constructor(@param:Type val type: Long,

if (orientation == RecyclerView.VERTICAL) {
if (spanCount == 1 || spanSize == spanCount) {
outRect.set(0, 0, 0, size)
if (itemPosition == 0 && showDivider == VisibleFactory.SHOW_TOP) {
outRect.set(0, size, 0, size)
} else {
outRect.set(0, 0, 0, size)
}
} else if (lineAccumulatedSpan == spanSize) {
outRect.set(0, 0, halfSize, size)
} else if (lineAccumulatedSpan == spanCount) {
Expand All @@ -412,7 +436,11 @@ class Divider constructor(@param:Type val type: Long,
}
} else {
if (spanCount == 1 || spanSize == spanCount) {
outRect.set(0, 0, size, 0)
if (itemPosition == 0 && showDivider == VisibleFactory.SHOW_TOP) {
outRect.set(size, 0, size, 0)
} else {
outRect.set(0, 0, size, 0)
}
} else if (lineAccumulatedSpan == spanSize) {
outRect.set(0, 0, size, halfSize)
} else if (lineAccumulatedSpan == spanCount) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@ abstract class VisibleFactory {
return LastItemInvisible()
}

fun getTopItemVisibleFactory(): VisibleFactory {
return TopItemVisible()
}

const val SHOW_NONE = 0L
const val SHOW_ITEMS_ONLY = 1L
const val SHOW_GROUP_ONLY = 2L
const val SHOW_ALL = 3L
const val SHOW_TOP = 4L

@IntDef(SHOW_NONE, SHOW_ITEMS_ONLY, SHOW_GROUP_ONLY, SHOW_ALL)
@IntDef(SHOW_NONE, SHOW_ITEMS_ONLY, SHOW_GROUP_ONLY, SHOW_ALL, SHOW_TOP)
@Retention(AnnotationRetention.SOURCE)
annotation class Show

Expand All @@ -48,6 +53,12 @@ abstract class VisibleFactory {
return if (groupIndex == groupCount - 1) SHOW_ITEMS_ONLY else SHOW_ALL
}
}

class TopItemVisible : VisibleFactory() {
override fun displayDividerForItem(groupCount: Int, groupIndex: Int): Long {
return if (groupIndex == 0) SHOW_TOP else SHOW_ALL
}
}
}

abstract fun displayDividerForItem(groupCount: Int, groupIndex: Int): Long
Expand Down

0 comments on commit 3b8cc1a

Please sign in to comment.