@@ -34,15 +34,15 @@ typealias ItemIndicatorWithPosition = Pair<FastScrollItemIndicator, Int>
3434 * @see FastScrollerThumbView
3535 */
3636class FastScrollerView @JvmOverloads constructor(
37- context : Context ,
38- attrs : AttributeSet ? = null ,
39- defStyleAttr : Int = R .attr.indicatorFastScrollerStyle,
40- defStyleRes : Int = R .style.Widget_IndicatorFastScroll_FastScroller
37+ context : Context ,
38+ attrs : AttributeSet ? = null ,
39+ defStyleAttr : Int = R .attr.indicatorFastScrollerStyle,
40+ defStyleRes : Int = R .style.Widget_IndicatorFastScroll_FastScroller
4141) : LinearLayout(
42- context,
43- attrs,
44- defStyleAttr,
45- defStyleRes
42+ context,
43+ attrs,
44+ defStyleAttr,
45+ defStyleRes
4646) {
4747
4848 var iconColor: ColorStateList ? by onUpdate(::bindItemIndicatorViews)
@@ -93,17 +93,18 @@ class FastScrollerView @JvmOverloads constructor(
9393
9494 init {
9595 context.theme.obtainStyledAttributes(
96- attrs,
97- R .styleable.FastScrollerView ,
98- defStyleAttr,
99- defStyleRes
96+ attrs,
97+ R .styleable.FastScrollerView ,
98+ defStyleAttr,
99+ defStyleRes
100100 ).use { attrsArray ->
101101 throwIfMissingAttrs(styleRes = R .style.Widget_IndicatorFastScroll_FastScroller ) {
102102 iconColor = attrsArray.getColorStateListOrThrow(R .styleable.FastScrollerView_iconColor )
103103 textAppearanceRes = attrsArray.getResourceIdOrThrow(
104- R .styleable.FastScrollerView_android_textAppearance
104+ R .styleable.FastScrollerView_android_textAppearance
105105 )
106- textColor = attrsArray.getColorStateListOrThrow(R .styleable.FastScrollerView_android_textColor )
106+ textColor = attrsArray
107+ .getColorStateListOrThrow(R .styleable.FastScrollerView_android_textColor )
107108 textPadding = attrsArray.getDimensionOrThrow(R .styleable.FastScrollerView_textPadding )
108109 }
109110 }
@@ -115,11 +116,11 @@ class FastScrollerView @JvmOverloads constructor(
115116
116117 if (isInEditMode) {
117118 itemIndicatorsWithPositions + = listOf (
118- ItemIndicatorWithPosition (FastScrollItemIndicator .Text (" A" ), 0 ),
119- ItemIndicatorWithPosition (FastScrollItemIndicator .Text (" B" ), 1 ),
120- ItemIndicatorWithPosition (FastScrollItemIndicator .Text (" C" ), 2 ),
121- ItemIndicatorWithPosition (FastScrollItemIndicator .Text (" D" ), 3 ),
122- ItemIndicatorWithPosition (FastScrollItemIndicator .Text (" E" ), 4 )
119+ ItemIndicatorWithPosition (FastScrollItemIndicator .Text (" A" ), 0 ),
120+ ItemIndicatorWithPosition (FastScrollItemIndicator .Text (" B" ), 1 ),
121+ ItemIndicatorWithPosition (FastScrollItemIndicator .Text (" C" ), 2 ),
122+ ItemIndicatorWithPosition (FastScrollItemIndicator .Text (" D" ), 3 ),
123+ ItemIndicatorWithPosition (FastScrollItemIndicator .Text (" E" ), 4 )
123124 )
124125 bindItemIndicatorViews()
125126 }
@@ -149,10 +150,10 @@ class FastScrollerView @JvmOverloads constructor(
149150 */
150151 @JvmOverloads
151152 fun setupWithRecyclerView (
152- recyclerView : RecyclerView ,
153- getItemIndicator : (Int ) -> FastScrollItemIndicator ? ,
154- showIndicator : ((FastScrollItemIndicator , Int , Int ) -> Boolean )? = null,
155- useDefaultScroller : Boolean = true
153+ recyclerView : RecyclerView ,
154+ getItemIndicator : (Int ) -> FastScrollItemIndicator ? ,
155+ showIndicator : ((FastScrollItemIndicator , Int , Int ) -> Boolean )? = null,
156+ useDefaultScroller : Boolean = true
156157 ) {
157158 if (isSetup) throw IllegalStateException (" Only set this view's RecyclerView once!" )
158159 this .recyclerView = recyclerView
@@ -162,24 +163,24 @@ class FastScrollerView @JvmOverloads constructor(
162163
163164 updateItemIndicators()
164165 val adapter = recyclerView.adapter ? : throw IllegalArgumentException (
165- " RecyclerView needs to have an adapter before setting up its fast scroller."
166+ " RecyclerView needs to have an adapter before setting up its fast scroller."
166167 )
167168 adapter.registerAdapterDataObserver(object : RecyclerView .AdapterDataObserver () {
168169 override fun onChanged () {
169170 postUpdateItemIndicators()
170171 }
171172
172173 override fun onItemRangeChanged (positionStart : Int , itemCount : Int , payload : Any? ) =
173- onChanged()
174+ onChanged()
174175
175176 override fun onItemRangeInserted (positionStart : Int , itemCount : Int ) =
176- onChanged()
177+ onChanged()
177178
178179 override fun onItemRangeMoved (fromPosition : Int , toPosition : Int , itemCount : Int ) =
179- onChanged()
180+ onChanged()
180181
181182 override fun onItemRangeRemoved (positionStart : Int , itemCount : Int ) =
182- onChanged()
183+ onChanged()
183184 })
184185 }
185186
@@ -198,8 +199,8 @@ class FastScrollerView @JvmOverloads constructor(
198199 private fun updateItemIndicators () {
199200 itemIndicatorsWithPositions.clear()
200201 itemIndicatorsBuilder
201- .buildItemIndicators(recyclerView!! , getItemIndicator, showIndicator)
202- .toCollection(itemIndicatorsWithPositions)
202+ .buildItemIndicators(recyclerView!! , getItemIndicator, showIndicator)
203+ .toCollection(itemIndicatorsWithPositions)
203204
204205 bindItemIndicatorViews()
205206 }
@@ -212,25 +213,25 @@ class FastScrollerView @JvmOverloads constructor(
212213 }
213214
214215 fun createIconView (iconIndicator : FastScrollItemIndicator .Icon ): ImageView =
215- (LayoutInflater .from(context).inflate(
216- R .layout.fast_scroller_indicator_icon, this , false
217- ) as ImageView ).apply {
218- iconColor?.let (::setImageTintList)
219- setImageResource(iconIndicator.iconRes)
220- tag = iconIndicator
221- }
216+ (LayoutInflater .from(context).inflate(
217+ R .layout.fast_scroller_indicator_icon, this , false
218+ ) as ImageView ).apply {
219+ iconColor?.let (::setImageTintList)
220+ setImageResource(iconIndicator.iconRes)
221+ tag = iconIndicator
222+ }
222223
223224 fun createTextView (textIndicators : List <FastScrollItemIndicator .Text >): TextView =
224- (LayoutInflater .from(context).inflate(
225- R .layout.fast_scroller_indicator_text, this , false
226- ) as TextView ).apply {
227- TextViewCompat .setTextAppearance(this , textAppearanceRes)
228- textColor?.let (::setTextColor)
229- updatePadding(top = textPadding.toInt(), bottom = textPadding.toInt())
230- setLineSpacing(textPadding, lineSpacingMultiplier)
231- text = textIndicators.joinToString(separator = " \n " ) { it.text }
232- tag = textIndicators
233- }
225+ (LayoutInflater .from(context).inflate(
226+ R .layout.fast_scroller_indicator_text, this , false
227+ ) as TextView ).apply {
228+ TextViewCompat .setTextAppearance(this , textAppearanceRes)
229+ textColor?.let (::setTextColor)
230+ updatePadding(top = textPadding.toInt(), bottom = textPadding.toInt())
231+ setLineSpacing(textPadding, lineSpacingMultiplier)
232+ text = textIndicators.joinToString(separator = " \n " ) { it.text }
233+ tag = textIndicators
234+ }
234235
235236 // Optimize the views by batching adjacent text indicators into a single TextView
236237 val viewCreators = ArrayList < () -> View > ()
@@ -239,8 +240,8 @@ class FastScrollerView @JvmOverloads constructor(
239240 while (index <= lastIndex) {
240241 @Suppress(" UNCHECKED_CAST" )
241242 val textIndicatorsBatch = subList(index, size)
242- .takeWhile { it is FastScrollItemIndicator .Text }
243- as List <FastScrollItemIndicator .Text >
243+ .takeWhile { it is FastScrollItemIndicator .Text }
244+ as List <FastScrollItemIndicator .Text >
244245 if (textIndicatorsBatch.isNotEmpty()) {
245246 viewCreators.add { createTextView(textIndicatorsBatch) }
246247 index + = textIndicatorsBatch.size
@@ -264,24 +265,24 @@ class FastScrollerView @JvmOverloads constructor(
264265 }
265266
266267 private fun selectItemIndicator (
267- indicator : FastScrollItemIndicator ,
268- indicatorCenterY : Int
268+ indicator : FastScrollItemIndicator ,
269+ indicatorCenterY : Int
269270 ) {
270271 val position = itemIndicatorsWithPositions
271- .first { it.first == indicator }
272- .let (ItemIndicatorWithPosition ::second)
272+ .first { it.first == indicator }
273+ .let (ItemIndicatorWithPosition ::second)
273274 if (position != lastSelectedPosition) {
274275 lastSelectedPosition = position
275276 if (useDefaultScroller) {
276277 scrollToPosition(position)
277278 }
278279 performHapticFeedback(
279- // Semantically, dragging across the indicators is similar to moving a text handle
280- if (Build .VERSION .SDK_INT >= 27 ) {
281- HapticFeedbackConstants .TEXT_HANDLE_MOVE
282- } else {
283- HapticFeedbackConstants .KEYBOARD_TAP
284- }
280+ // Semantically, dragging across the indicators is similar to moving a text handle
281+ if (Build .VERSION .SDK_INT >= 27 ) {
282+ HapticFeedbackConstants .TEXT_HANDLE_MOVE
283+ } else {
284+ HapticFeedbackConstants .KEYBOARD_TAP
285+ }
285286 )
286287 itemIndicatorSelectedCallbacks.forEach {
287288 it.onItemIndicatorSelected(indicator, indicatorCenterY, position)
@@ -324,13 +325,13 @@ class FastScrollerView @JvmOverloads constructor(
324325 val textIndicatorsTouchY = touchY - view.top
325326 val textLineHeight = view.height / possibleTouchedIndicators.size
326327 val touchedIndicatorIndex = Math .min(
327- textIndicatorsTouchY / textLineHeight,
328- possibleTouchedIndicators.lastIndex
328+ textIndicatorsTouchY / textLineHeight,
329+ possibleTouchedIndicators.lastIndex
329330 )
330331 val touchedIndicator = possibleTouchedIndicators[touchedIndicatorIndex]
331332
332333 val centerY = view.y.toInt() +
333- (textLineHeight / 2 ) + (touchedIndicatorIndex * textLineHeight)
334+ (textLineHeight / 2 ) + (touchedIndicatorIndex * textLineHeight)
334335 selectItemIndicator(touchedIndicator, centerY)
335336 consumed = true
336337 }
@@ -341,15 +342,14 @@ class FastScrollerView @JvmOverloads constructor(
341342 isPressed = consumed
342343
343344 onItemIndicatorTouched?.invoke(consumed)
344- return consumed;
345+ return consumed
345346 }
346347
347348 interface ItemIndicatorSelectedCallback {
348349 fun onItemIndicatorSelected (
349- indicator : FastScrollItemIndicator ,
350- indicatorCenterY : Int ,
351- itemPosition : Int
350+ indicator : FastScrollItemIndicator ,
351+ indicatorCenterY : Int ,
352+ itemPosition : Int
352353 )
353354 }
354-
355355}
0 commit comments