Skip to content

Commit

Permalink
Merge pull request #6 from lriccardo/dev
Browse files Browse the repository at this point in the history
Release 1.0.5
  • Loading branch information
lriccardo authored Dec 19, 2021
2 parents 41073df + 2adaa95 commit ee5149e
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 30 deletions.
40 changes: 33 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,16 @@ dependencies {
<td>12dp</td>
</tr>
</tr>
<tr>
<tr>
<td>app:indicator_color</td>
<td>Color</td>
<td>Color.RED</td>
</tr>
<tr>
<td>app:indicator_y_position</td>
<td>Percentage (0.0 - 1.0)</td>
<td>0.5f</td>
</tr>
<tr>
<td>app:checked_indicator_size</td>
<td>Dimension</td>
Expand Down Expand Up @@ -160,6 +165,11 @@ recyclerView.addItemDecoration(
<td>Float</td>
<td>24f</td>
</tr>
<tr>
<td>indicatorYPosition</td>
<td>Percentage (0.0f - 1.0f)</td>
<td>0.5f</td>
</tr>
<tr>
<td>checkedIndicatorSize</td>
<td>Float</td>
Expand All @@ -175,11 +185,26 @@ recyclerView.addItemDecoration(
<td>Normal (Normal | Dashed)</td>
<td>TimelineView default value</td>
</tr>
<tr>
<td>linePadding</td>
<td>Float</td>
<td>TimelineView default value</td>
</tr>
<tr>
<td>lineDashLength</td>
<td>Float</td>
<td>TimelineView default value</td>
</tr>
<tr>
<td>lineDashGap</td>
<td>Float</td>
<td>TimelineView default value</td>
</tr>
<tr>
<td>lineWidth</td>
<td>Float</td>
<td>TimelineView default value</td>
</tr>
</tr>
<tr>
<td>padding</td>
<td>Float</td>
Expand Down Expand Up @@ -209,10 +234,11 @@ recyclerView.addItemDecoration(
Implementing one or all of these methods, allows you to use the `position` argument to return a different customization for some of your items.
```kotlin
interface TimelineAdapter {
fun getTimelineViewType(position: Int): TimelineView.ViewType
fun getIndicatorStyle(position: Int): TimelineView.IndicatorStyle
fun getIndicatorColor(position: Int): Int
fun getLineColor(position: Int): Int
fun getLineStyle(position: Int): TimelineView.LineStyle
fun getTimelineViewType(position: Int): TimelineView.ViewType?
fun getIndicatorStyle(position: Int): TimelineView.IndicatorStyle?
fun getIndicatorColor(position: Int): Int?
fun getLineColor(position: Int): Int?
fun getLineStyle(position: Int): TimelineView.LineStyle?
fun getLinePadding(position: Int): Float?
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ interface TimelineAdapter {
fun getIndicatorColor(position: Int): Int? = null
fun getLineColor(position: Int): Int? = null
fun getLineStyle(position: Int): TimelineView.LineStyle? = null
fun getLinePadding(position: Int): Float? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ import androidx.recyclerview.widget.RecyclerView
class TimelineDecorator(
val indicatorStyle: TimelineView.IndicatorStyle = TimelineView.IndicatorStyle.Filled,
val indicatorSize: Float = 12.toPx().toFloat(),
val indicatorYPosition: Float = 0.5f,
val checkedIndicatorSize: Float? = null,
val checkedIndicatorStrokeWidth: Float = 4.toPx().toFloat(),
val lineStyle: TimelineView.LineStyle? = null,
val linePadding: Float? = null,
val lineDashLength: Float? = null,
val lineDashGap: Float? = null,
val lineWidth: Float? = null,
val padding: Float = 16.toPx().toFloat(),
val position: Position = Position.Left,
Expand Down Expand Up @@ -75,17 +79,31 @@ class TimelineDecorator(
(getLineStyle(itemPosition) ?: lineStyle)?.let {
timelineView.lineStyle = it
}

(getLinePadding(itemPosition) ?: linePadding)?.let {
timelineView.linePadding = it
}
}
timelineView.indicatorSize = indicatorSize

timelineView.indicatorYPosition = indicatorYPosition

checkedIndicatorSize?.let {
timelineView.checkedIndicatorSize = it
}

checkedIndicatorStrokeWidth?.let {
checkedIndicatorStrokeWidth.let {
timelineView.checkedIndicatorStrokeWidth = it
}

lineDashLength?.let {
timelineView.lineDashLength = it
}

lineDashGap?.let {
timelineView.lineDashGap = it
}

lineWidth?.let {
timelineView.lineWidth = it
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class TimelineView @JvmOverloads constructor(
initIndicatorPaint()
}

var indicatorYPosition: Float = 0.5f
set(value) {
field = value.coerceIn(0f, 1f)
}

var lineStyle = LineStyle.Normal
set(value) {
field = value
Expand All @@ -65,6 +70,12 @@ class TimelineView @JvmOverloads constructor(
field = value
initLinePaint()
}
var linePadding: Float = 0.toPx().toFloat()
set(value) {
field = value
initLinePaint()
}

var lineDashLength: Float = 18.toPx().toFloat()
set(value) {
field = value
Expand Down Expand Up @@ -112,11 +123,26 @@ class TimelineView @JvmOverloads constructor(
checkedIndicatorSize.toInt()
).toFloat()

checkedIndicatorStrokeWidth = getDimensionPixelSize(
R.styleable.TimelineView_checked_indicator_stroke_width,
checkedIndicatorStrokeWidth.toInt()
).toFloat()

indicatorYPosition = getFloat(
R.styleable.TimelineView_indicator_y_position,
indicatorYPosition
).coerceIn(0f, 1f)

lineWidth = getDimensionPixelSize(
R.styleable.TimelineView_line_width,
lineWidth.toInt()
).toFloat()

linePadding = getDimensionPixelSize(
R.styleable.TimelineView_line_padding,
linePadding.toInt()
).toFloat()

lineDashLength = getDimensionPixelSize(
R.styleable.TimelineView_line_dash_length,
lineDashLength.toInt()
Expand All @@ -132,11 +158,6 @@ class TimelineView @JvmOverloads constructor(

lineColor = getColor(R.styleable.TimelineView_line_color, lineColor)

checkedIndicatorStrokeWidth = getDimensionPixelSize(
R.styleable.TimelineView_checked_indicator_stroke_width,
checkedIndicatorStrokeWidth.toInt()
).toFloat()

indicatorColor = getColor(R.styleable.TimelineView_indicator_color, indicatorColor)
indicatorStyle =
IndicatorStyle.values()[getInteger(R.styleable.TimelineView_indicator_style, indicatorStyle.ordinal)]
Expand Down Expand Up @@ -213,7 +234,8 @@ class TimelineView @JvmOverloads constructor(
var bottomLineYEnd: Float

val indicatorCenterX = (width / 2).toFloat()
val indicatorCenterY = (height / 2).toFloat()
val indicatorCenterY =
(height * indicatorYPosition).coerceIn(indicatorSize, height - indicatorSize)

var drawIndicator = true
var drawTopLine = false
Expand Down Expand Up @@ -243,14 +265,16 @@ class TimelineView @JvmOverloads constructor(
}

topLineYStart = 0f
if(lineStyle == LineStyle.Dashed)
if (lineStyle == LineStyle.Dashed && (indicatorCenterY - indicatorSize) > lineDashGap)
topLineYStart += lineDashGap


bottomLineYStart = height.toFloat()
if(drawIndicator) {
topLineYEnd = (indicatorCenterY - indicatorSize) + 1f
bottomLineYEnd = (indicatorCenterY + indicatorSize) - 1f
topLineYEnd =
(indicatorCenterY - indicatorSize - linePadding).coerceAtLeast(topLineYStart)
bottomLineYEnd =
(indicatorCenterY + indicatorSize + linePadding).coerceAtMost(bottomLineYStart)
} else {
topLineYEnd = indicatorCenterY
bottomLineYEnd = indicatorCenterY
Expand Down
2 changes: 2 additions & 0 deletions TimelineView/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</attr>
<attr name="indicator_size" format="dimension"/>
<attr name="indicator_color" format="color"/>
<attr name="indicator_y_position" format="float"/>

<attr name="checked_indicator_size" format="dimension"/>
<attr name="checked_indicator_stroke_width" format="dimension"/>
Expand All @@ -28,5 +29,6 @@

<attr name="line_dash_length" format="dimension"/>
<attr name="line_dash_gap" format="dimension"/>
<attr name="line_padding" format="dimension"/>
</declare-styleable>
</resources>
Binary file added screens/expand.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.lriccardo.timelineview_example.activities

import androidx.appcompat.app.AppCompatActivity
import android.content.res.Resources.Theme
import android.os.Bundle
import android.util.TypedValue
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lriccardo.timelineview.TimelineDecorator
import com.lriccardo.timelineview_example.R
import com.lriccardo.timelineview_example.adapters.BaseAdapter
import com.lriccardo.timelineview_example.databinding.ActivityRecyclerViewDecoratorBinding

Expand All @@ -18,7 +22,30 @@ class RecyclerViewDecoratorActivity : AppCompatActivity() {
binding.timelineRv.let {
it.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
it.adapter = BaseAdapter((0..10).toList())
it.addItemDecoration(TimelineDecorator())

val colorPrimary = TypedValue()
val theme: Theme = getTheme()
theme.resolveAttribute(R.attr.colorPrimary, colorPrimary, true)

it.addItemDecoration(
TimelineDecorator(
position = TimelineDecorator.Position.Left,
indicatorColor = colorPrimary.data,
lineColor = colorPrimary.data
)
)

it.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
(it.layoutManager as? LinearLayoutManager)?.let {
if (it.findFirstCompletelyVisibleItemPosition() == 0)
binding.fab.extend()
else
binding.fab.shrink()
}
}
})
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.lriccardo.timelineview_example.adapters

import android.content.res.Resources
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.lriccardo.timelineview.TimelineAdapter
import com.lriccardo.timelineview.TimelineView
import com.lriccardo.timelineview_example.R
import com.lriccardo.timelineview_example.viewholders.BaseViewHolder

class BaseAdapter(val items: List<Int>): RecyclerView.Adapter<BaseViewHolder>() {
class BaseAdapter(val items: List<Int>) : RecyclerView.Adapter<BaseViewHolder>(), TimelineAdapter {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.base_list_item, parent, false)
Expand All @@ -22,4 +27,26 @@ class BaseAdapter(val items: List<Int>): RecyclerView.Adapter<BaseViewHolder>()
}

override fun getItemCount(): Int = items.size

override fun getIndicatorStyle(position: Int): TimelineView.IndicatorStyle? {
if (position <= 1)
return TimelineView.IndicatorStyle.Checked
else return TimelineView.IndicatorStyle.Empty
}

override fun getLineStyle(position: Int): TimelineView.LineStyle? {
if (position > 1)
return TimelineView.LineStyle.Dashed
return super.getLineStyle(position)
}

override fun getLinePadding(position: Int): Float? {
if (position > 1)
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
16f,
Resources.getSystem().displayMetrics
)
return super.getLinePadding(position)
}
}
5 changes: 5 additions & 0 deletions timelineview_example/src/main/res/drawable/ic_settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M19.14,12.94c0.04,-0.3 0.06,-0.61 0.06,-0.94c0,-0.32 -0.02,-0.64 -0.07,-0.94l2.03,-1.58c0.18,-0.14 0.23,-0.41 0.12,-0.61l-1.92,-3.32c-0.12,-0.22 -0.37,-0.29 -0.59,-0.22l-2.39,0.96c-0.5,-0.38 -1.03,-0.7 -1.62,-0.94L14.4,2.81c-0.04,-0.24 -0.24,-0.41 -0.48,-0.41h-3.84c-0.24,0 -0.43,0.17 -0.47,0.41L9.25,5.35C8.66,5.59 8.12,5.92 7.63,6.29L5.24,5.33c-0.22,-0.08 -0.47,0 -0.59,0.22L2.74,8.87C2.62,9.08 2.66,9.34 2.86,9.48l2.03,1.58C4.84,11.36 4.8,11.69 4.8,12s0.02,0.64 0.07,0.94l-2.03,1.58c-0.18,0.14 -0.23,0.41 -0.12,0.61l1.92,3.32c0.12,0.22 0.37,0.29 0.59,0.22l2.39,-0.96c0.5,0.38 1.03,0.7 1.62,0.94l0.36,2.54c0.05,0.24 0.24,0.41 0.48,0.41h3.84c0.24,0 0.44,-0.17 0.47,-0.41l0.36,-2.54c0.59,-0.24 1.13,-0.56 1.62,-0.94l2.39,0.96c0.22,0.08 0.47,0 0.59,-0.22l1.92,-3.32c0.12,-0.22 0.07,-0.47 -0.12,-0.61L19.14,12.94zM12,15.6c-1.98,0 -3.6,-1.62 -3.6,-3.6s1.62,-3.6 3.6,-3.6s3.6,1.62 3.6,3.6S13.98,15.6 12,15.6z"/>
</vector>
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.coordinatorlayout.widget.CoordinatorLayout 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="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/timeline_rv"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/timeline_rv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_gravity="bottom|end"
android:text="@string/configure"
app:icon="@drawable/ic_settings"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
1 change: 1 addition & 0 deletions timelineview_example/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<resources>
<string name="app_name">TimelineView Example</string>
<string name="configure">Configure</string>
</resources>

0 comments on commit ee5149e

Please sign in to comment.