IndicatorFastScroll icon indicating copy to clipboard operation
IndicatorFastScroll copied to clipboard

Error while trying to set up custom scroll

Open ibrajix opened this issue 4 years ago • 5 comments

This is what I want: On click of any alphabet, it should jump to the item in the list instead of scrolling (Default behaviour)

I have implemented custom scrolling like this as per the doc.

` binding.rcvAllArtistes.adapter = allArtisteAdapter
    binding.rcvAllArtistes.layoutManager = LinearLayoutManager(context)
    binding.fastscroller.setupWithRecyclerView(
        binding.rcvAllArtistes,
        { position ->
            val item = allArtisteAdapter.artistes[position] // Get your model object
            // or fetch the section at [position] from your database
            FastScrollItemIndicator.Text(
                item.artisteName?.substring(0, 1)!!.toUpperCase(Locale.ROOT) // Grab the first letter and capitalize it
            ) // Return a text indicator
        }
    )
    binding.fastscroller.useDefaultScroller = false

val smoothScroller: LinearSmoothScroller = object : LinearSmoothScroller(context) {
        override fun getVerticalSnapPreference(): Int = SNAP_TO_START
    }

val linearLayoutManager = LinearLayoutManager(context)

binding.fastscroller.itemIndicatorSelectedCallbacks  += object : FastScrollerView.ItemIndicatorSelectedCallback {
        override fun onItemIndicatorSelected(
            indicator: FastScrollItemIndicator,
            indicatorCenterY: Int,
            itemPosition: Int
        ) {
            binding.rcvAllArtistes.stopScroll()
            smoothScroller.targetPosition = itemPosition
            linearLayoutManager.startSmoothScroll(smoothScroller)
        }
    }

    binding.fastscrollerThumb.setupWithFastScroller(binding.fastscroller) `

When I click on the scrollbar, I get ERROR:

java.lang.NullPointerException: Attempt to read from field 'androidx.recyclerview.widget.RecyclerView$ViewFlinger androidx.recyclerview.widget.RecyclerView.mViewFlinger' on a null object reference

Also, how to change the size of the alphabets shown in scrollbar. No textSize attribute.

ibrajix avatar May 21 '21 08:05 ibrajix

I'm not sure if this is the issue, but You're first setting linear layout manager here:

binding.rcvAllArtistes.layoutManager = LinearLayoutManager(context)

then You are attempting to start the scrolling from another linearLayoutManager, not the one attached to the recycler view

val linearLayoutManager = LinearLayoutManager(context)

I'd try to get layout manager from recycler view instead

recyclerView.layoutManager.startSmoothScroll(RecyclerView.SmoothScroller smoothScroller)

enricocid avatar May 21 '21 08:05 enricocid

You can theme indicator's text as follows:


In your base theme:
<item name="indicatorFastScrollerStyle">@style/FastScrollerTheme</item>

Define FastScrollerTheme:

<style name="FastScrollerTheme" parent="Widget.IndicatorFastScroll.FastScroller">       
    <item name="android:textColor">@color/your_color</item>;        
    <item name="android:textAppearance">@style/FastScrollerTextStyle</item>
</style>

<style name="FastScrollerTextStyle">       
    <item name="android:textStyle">bold</item>
    <item name="android:textSize">...</item>
</style>

I'm personally extending TextAppearance.Material.* theme so I don't have to deal with dimensions

<style name="FastScrollerTextStyle" parent="@android:style/TextAppearance.Material.Caption">       
     ...
</style>

You can change parent to TextAppearance.Material.Body1 or TextAppearance.Material.Body2 or TextAppearance.Material.Title or TextAppearance.Material.Headline ...

enricocid avatar May 21 '21 09:05 enricocid

Still getting thesame scroll behaviour

I tried this way:

binding.rcvAllArtistes.layoutManager?.startSmoothScroll(smoothScroller)

I just want to jump to the list instead of showing that scrolling effect...

ibrajix avatar May 21 '21 12:05 ibrajix

Still getting thesame scroll behaviour

I tried this way:

binding.rcvAllArtistes.layoutManager?.startSmoothScroll(smoothScroller)

I just want to jump to the list instead of showing that scrolling effect...

I'm doing this in my app if You're interested:

override fun onItemIndicatorSelected(
    indicator: FastScrollItemIndicator, 
    indicatorCenterY: Int,
    itemPosition: Int
) {
    val linearLayoutManager = recyclerView.layoutManager as LinearLayoutManager                            
    linearLayoutManager.scrollToPositionWithOffset(itemPosition, 0)
}

:)

enricocid avatar May 21 '21 15:05 enricocid

Still getting thesame scroll behaviour I tried this way: binding.rcvAllArtistes.layoutManager?.startSmoothScroll(smoothScroller) I just want to jump to the list instead of showing that scrolling effect...

I'm doing this in my app if You're interested:

override fun onItemIndicatorSelected(
    indicator: FastScrollItemIndicator, 
    indicatorCenterY: Int,
    itemPosition: Int
) {
    val linearLayoutManager = recyclerView.layoutManager as LinearLayoutManager                            
    linearLayoutManager.scrollToPositionWithOffset(itemPosition, 0)
}

:)

WORKED. THANK YOU

ibrajix avatar May 21 '21 21:05 ibrajix