Skip to content

Commit

Permalink
Merge pull request #12 from SodaLabs/issue/srv-7-supportGridLayout
Browse files Browse the repository at this point in the history
Support grid layout for production and preview.
  • Loading branch information
tcw165 authored Oct 19, 2018
2 parents a1ab473 + b9ca481 commit 8048fdd
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
18 changes: 18 additions & 0 deletions app-demo/src/main/res/layout/grid_view_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="70dp"
android:layout_height="70dp">

<android.support.v7.widget.AppCompatImageView
android:id="@+id/imageView2"
android:layout_width="50dp"
android:layout_height="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@tools:sample/avatars" />
</android.support.constraint.ConstraintLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView

Expand Down Expand Up @@ -37,6 +38,13 @@ class DrawableDividerDecoration(
else -> throw IllegalStateException("Unknown orientation")
}
}
is GridLayoutManager -> {
when (layoutManager.orientation) {
RecyclerView.HORIZONTAL -> decorateHorizontally(canvas, parent)
RecyclerView.VERTICAL -> decorateVertically(canvas, parent)
else -> throw IllegalStateException("Unknown orientation")
}
}
else -> throw IllegalStateException(
"Cannot support ${javaClass.simpleName} for ${layoutManager.javaClass.simpleName}")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,52 @@
package co.sodalabs.view

import android.content.Context
import android.content.res.TypedArray
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.AttributeSet
import co.sodalabs.R
import co.sodalabs.view.StyledRecyclerView.Companion.PREVIEW_LAYOUT_AS_GRID
import co.sodalabs.view.StyledRecyclerView.Companion.PREVIEW_LAYOUT_AS_LIST

/**
* A custom [RecyclerView] supporting the style attribute [R.styleable.StyledRecyclerView].
*
* @see [R.attr.rvDividerDrawable] The drawable reference.
* @see [R.attr.rvDividerMode] There are [DividerMode.SHOW_DIVIDER_BEGINNING],
* [DividerMode.SHOW_DIVIDER_MIDDLE], [DividerMode.SHOW_DIVIDER_END] and [DividerMode.SHOW_DIVIDER_NONE].
* @see [R.attr.rvPreviewLayout] There are [PREVIEW_LAYOUT_AS_LIST] and [PREVIEW_LAYOUT_AS_GRID].
*/
open class StyledRecyclerView : RecyclerView {

companion object {

/**
* To preview the item displayed in a list in XML previewer.
*/
@JvmStatic
val PREVIEW_LAYOUT_AS_LIST = 0
/**
* To preview the item displayed in a grid in XML previewer.
*/
@JvmStatic
val PREVIEW_LAYOUT_AS_GRID = 1
}

constructor(context: Context) : this(context, null)
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(context, attrs, defStyle) {
val a = context.theme.obtainStyledAttributes(
attrs,
R.styleable.StyledRecyclerView, 0, 0)
decorateView(a, getOrientation(attrs))
a.recycle()
decorateView(getOrientation(attrs), attrs)
}

private fun decorateView(
typedArray: TypedArray,
orientation: Int
orientation: Int,
attrs: AttributeSet?
) {
val typedArray = context.theme.obtainStyledAttributes(
attrs,
R.styleable.StyledRecyclerView, 0, 0)

// Divider and the mode
if (typedArray.hasValue(R.styleable.StyledRecyclerView_rvDividerDrawable)) {
val dividerDrawable = typedArray.getDrawable(R.styleable.StyledRecyclerView_rvDividerDrawable)
Expand All @@ -47,8 +64,18 @@ open class StyledRecyclerView : RecyclerView {

// Preview list item
if (isInEditMode) {
layoutManager = LinearLayoutManager(context, orientation, false)
// In XML, you could add attribute "app:rvPreviewLayout=grid" to
// display the items in a grid
val layout = typedArray.getInt(R.styleable.StyledRecyclerView_rvPreviewLayout, PREVIEW_LAYOUT_AS_LIST)
val layoutManager = when (layout) {
PREVIEW_LAYOUT_AS_GRID -> GridLayoutManager(context, 3, orientation, false)
else -> LinearLayoutManager(context, orientation, false)
}

this.layoutManager = layoutManager
}

typedArray.recycle()
}

private fun getOrientation(attrs: AttributeSet?): Int {
Expand All @@ -59,4 +86,4 @@ open class StyledRecyclerView : RecyclerView {
b.recycle()
return orientation
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="foo" format="integer" />

<declare-styleable name="StyledRecyclerView">
<attr name="rvDividerDrawable" format="reference" />
<attr name="rvDividerMode">
Expand All @@ -8,5 +10,9 @@
<flag name="middle" value="2" />
<flag name="end" value="4" />
</attr>
<attr name="rvPreviewLayout">
<enum name="list" value="0" />
<enum name="grid" value="1" />
</attr>
</declare-styleable>
</resources>

0 comments on commit 8048fdd

Please sign in to comment.