-
Notifications
You must be signed in to change notification settings - Fork 52
/
CardSliderIndicator.kt
277 lines (212 loc) · 9.08 KB
/
CardSliderIndicator.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package com.github.islamkhsh
import android.animation.LayoutTransition
import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.github.islamkhsh.CardSliderIndicator.IndicatorState.*
import com.github.islamkhsh.CardSliderIndicator.SwipeDirection.TO_END
import com.github.islamkhsh.CardSliderIndicator.SwipeDirection.TO_START
import com.github.islamkhsh.viewpager2.ViewPager2
import com.github.islamkhsh.viewpager2.ViewPager2.ORIENTATION_VERTICAL
import kotlin.math.min
class CardSliderIndicator : LinearLayout {
companion object {
const val UNLIMITED_INDICATORS = -1
}
private val pageChangeListener = object : ViewPager2.OnPageChangeCallback(){
override fun onPageScrollStateChanged(state: Int) {}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
override fun onPageSelected(position: Int) {
if (position > selectedPosition)
swipeDirection = TO_END
else if (position < selectedPosition)
swipeDirection = TO_START
changeIndicatorsDisplayingState(position)
for (i in 0 until childCount) {
if (i == position)
changeIndicatorState(i, selectedIndicator!!)
else
changeIndicatorState(i, defaultIndicator!!)
}
selectedPosition = position
}
}
private var selectedPosition = 0
private var swipeDirection = TO_END
private var displayingRang = 0..0
internal var viewPager: CardSliderViewPager? = null
set(value) {
field = value
setupWithViewCardSliderViewPager()
}
/**
* default indicator drawable, the background of the view if not selected
*/
var defaultIndicator: Drawable? = null
set(value) {
field = value ?: ContextCompat.getDrawable(context, R.drawable.default_dot)
}
/**
* selected indicator drawable, the background of the view if selected
*/
var selectedIndicator: Drawable? = null
set(value) {
field = value ?: ContextCompat.getDrawable(context, R.drawable.selected_dot)
}
/**
* space between one indicator and the next one
*/
var indicatorMargin = 0f
/**
* max number of indicators to show and others will be hidden , -1 to show all
*/
var indicatorsToShow = UNLIMITED_INDICATORS
set(value) {
field = value
viewPager?.currentItem = 0
setupWithViewCardSliderViewPager()
}
constructor(context: Context) : super(context) {
initIndicatorGroup(null)
}
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
initIndicatorGroup(attrs)
}
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
context,
attrs,
defStyleAttr
) {
initIndicatorGroup(attrs)
}
private fun initIndicatorGroup(attrs: AttributeSet?) {
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.CardSliderIndicator)
defaultIndicator = typedArray.getDrawable(R.styleable.CardSliderIndicator_defaultIndicator)
selectedIndicator =
typedArray.getDrawable(R.styleable.CardSliderIndicator_selectedIndicator)
indicatorMargin = typedArray.getDimension(
R.styleable.CardSliderIndicator_indicatorMargin,
min(defaultIndicator!!.intrinsicWidth, selectedIndicator!!.intrinsicWidth).toFloat()
)
indicatorsToShow = typedArray.getInt(R.styleable.CardSliderIndicator_indicatorsToShow, -1)
typedArray.recycle()
if (indicatorsToShow != UNLIMITED_INDICATORS)
displayingRang = 0 until indicatorsToShow
orientation = HORIZONTAL
gravity = Gravity.CENTER_VERTICAL
layoutTransition = LayoutTransition()
}
private fun setupWithViewCardSliderViewPager() {
viewPager?.adapter?.run {
removeAllViews()
// create indicators
for (i in 0 until itemCount) {
addView(Indicator(context), i)
}
pageChangeListener.onPageSelected(viewPager!!.currentItem)
viewPager?.unregisterOnPageChangeCallback(pageChangeListener)
viewPager?.registerOnPageChangeCallback(pageChangeListener)
registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver(){
override fun onChanged() {
setupWithViewCardSliderViewPager()
}
override fun onItemRangeChanged(positionStart: Int, itemCount: Int) {
setupWithViewCardSliderViewPager()
}
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
setupWithViewCardSliderViewPager()
}
override fun onItemRangeMoved(fromPosition: Int, toPosition: Int, itemCount: Int) {
setupWithViewCardSliderViewPager()
}
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
setupWithViewCardSliderViewPager()
}
})
}
}
private fun changeIndicatorState(indicatorPosition: Int, drawableState: Drawable) {
(getChildAt(indicatorPosition) as Indicator).run {
changeIndicatorDrawableState(drawableState)
changeIndicatorAppearanceState(indicatorPosition)
}
}
private fun changeIndicatorsDisplayingState(currentPosition: Int) {
if (currentPosition == 0)
displayingRang = 0 until indicatorsToShow
else if (currentPosition == displayingRang.first && swipeDirection == TO_START)
displayingRang = displayingRang.decrement()
else if (currentPosition == displayingRang.last && swipeDirection == TO_END)
displayingRang = displayingRang.increment(childCount - 1)
}
private inner class Indicator : View {
private val infiniteScaleFactor = 0.5f
private var state = NORMAL
set(value) {
field = if (indicatorsToShow == UNLIMITED_INDICATORS) NORMAL else value
when (field) {
NORMAL -> {
layoutParams = (layoutParams as MarginLayoutParams).apply {
marginEnd = indicatorMargin.toInt()
}
scaleX = 1f
scaleY = 1f
visibility = VISIBLE
}
HIDDEN -> visibility = GONE
LAST -> {
layoutParams = (layoutParams as MarginLayoutParams).apply { marginEnd = 0 }
scaleX = 1f
scaleY = 1f
visibility = VISIBLE
}
INFINITE_START -> {
layoutParams = (layoutParams as MarginLayoutParams).apply {
marginEnd = indicatorMargin.toInt()
}
scaleX = infiniteScaleFactor
scaleY = infiniteScaleFactor
visibility = VISIBLE
}
INFINITE_END -> {
layoutParams = (layoutParams as MarginLayoutParams).apply { marginEnd = 0 }
scaleX = infiniteScaleFactor
scaleY = infiniteScaleFactor
visibility = VISIBLE
}
}
}
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
context, attrs, defStyleAttr
)
fun changeIndicatorDrawableState(drawableState: Drawable) {
background = drawableState
layoutParams = LayoutParams(drawableState.intrinsicWidth, drawableState.intrinsicHeight)
}
fun changeIndicatorAppearanceState(indicatorPosition: Int) {
val lastPosition = childCount - 1
state = when {
indicatorPosition != 0 && indicatorPosition == displayingRang.first -> INFINITE_START
indicatorPosition != lastPosition && indicatorPosition == displayingRang.last -> INFINITE_END
indicatorPosition == lastPosition && indicatorPosition in displayingRang -> LAST
indicatorPosition in displayingRang -> NORMAL
else -> HIDDEN
}
}
}
private enum class SwipeDirection { TO_END, TO_START }
private enum class IndicatorState {
NORMAL,
HIDDEN,
LAST,
INFINITE_START,
INFINITE_END,
}
}