Skip to content

How to add ItemDecorator ? #5

@aalexsanyan

Description

@aalexsanyan

Hi,
I found quite useful, thanks so much,
I tried to use your code ,I have noticed that the item count in adapter = item.count + 2 (the first and the last one) I wanted to have itemdecorator and I used the one I have in my project which is working as expected, but as I used it here, the dots count become +2 as expected,
I set the itemcount in ItemDecorator -2
and some code but it crashes. Here is my code.
Please have a look .
I have changed the code here commenting the code which I has previously

//itemCount = parent.adapter.itemCount
itemCount = parent.adapter.itemCount-2

// val activePosition = layoutManager.findFirstVisibleItemPosition()
var activePosition = layoutManager.findFirstVisibleItemPosition()
if (layoutManager.findFirstVisibleItemPosition() > 0 && layoutManager.findFirstVisibleItemPosition() % (itemCount ) == 0) {
activePosition = 1
}

`
import android.content.Context
import android.content.res.Resources
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator

class CirclePagerIndicatorDecoration(val context: Context, private val colorActive: Int, private val colorInactive: Int, private var itemCount: Int = 0) : RecyclerView.ItemDecoration() {

private val indicatorHeight = (DP * 16).toInt()
private val bottomMargin = (DP * 16).toInt()
private val indicatorStrokeWidth = DP * 2
private val indicatorItemLength = DP * 16
private val indicatorItemPadding = DP * 2
private val interpolator = AccelerateDecelerateInterpolator()

private val paint = Paint()

init {
paint.strokeCap = Paint.Cap.ROUND
paint.strokeWidth = indicatorStrokeWidth
paint.style = Paint.Style.FILL
paint.isAntiAlias = true
}

override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
super.onDrawOver(c, parent, state)
//itemCount = parent.adapter.itemCount
itemCount = parent.adapter.itemCount-2

`
// center horizontally, calculate width and subtract half from center
val totalLength = indicatorItemLength * itemCount
val paddingBetweenItems = Math.max(0, itemCount - 1) * indicatorItemPadding
val indicatorTotalWidth = totalLength + paddingBetweenItems
val indicatorStartX = (parent.width - indicatorTotalWidth) / 2f
val indicatorPosY = parent.height - bottomMargin - indicatorHeight / 2f
drawInactiveIndicators(c, indicatorStartX, indicatorPosY, itemCount)

val layoutManager = parent.layoutManager as LinearLayoutManager

// val activePosition = layoutManager.findFirstVisibleItemPosition()

var activePosition = layoutManager.findFirstVisibleItemPosition()
if (layoutManager.findFirstVisibleItemPosition() > 0 && layoutManager.findFirstVisibleItemPosition() % (itemCount ) == 0) {
  activePosition = 1
}
val activeChild = layoutManager.findViewByPosition(activePosition)
if (activePosition == RecyclerView.NO_POSITION) {
  return
}

val left = activeChild.left
val width = activeChild.width
val progress = interpolator.getInterpolation(left * -1 / width.toFloat())

drawHighlights(c, indicatorStartX, indicatorPosY, activePosition, progress, itemCount)

}

private fun drawInactiveIndicators(c: Canvas, indicatorStartX: Float, indicatorPosY: Float, itemCount: Int) {
paint.color = colorInactive

// width of item indicator including padding
val itemWidth = indicatorItemLength + indicatorItemPadding

var start = indicatorStartX
for (i in 0 until itemCount) {
  c.drawCircle(start + indicatorItemLength / 2, indicatorPosY, itemWidth / 5, paint)
  start += itemWidth
}

}

private fun drawHighlights(c: Canvas, indicatorStartX: Float, indicatorPosY: Float,
highlightPosition: Int, progress: Float, itemCount: Int) {
paint.color = colorActive
val itemWidth = indicatorItemLength + indicatorItemPadding

if (progress == 0f) {
  val highlightStart = indicatorStartX + itemWidth * highlightPosition
  c.drawCircle(highlightStart + indicatorItemLength / 2, indicatorPosY, itemWidth / 5, paint)
} else {
  val highlightStart = indicatorStartX + itemWidth * highlightPosition
  val partialLength = (indicatorItemLength) * progress
  c.drawCircle(highlightStart + partialLength + indicatorItemLength / 2, indicatorPosY, itemWidth / 5, paint)
}

}

override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
super.getItemOffsets(outRect, view, parent, state)
outRect.bottom = indicatorHeight
}

companion object {
private val DP = Resources.getSystem().displayMetrics.density
}
}`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions