0

I want the Recyclerview to auto-scroll by certain Dp. I tried using a scrollTo(xoffset,yoffset) but it does not

    recylerView=getDataBinder().ivOnboard
        adapter= CarouselAdapter(binder.root.context,configHelper.getAppConfig()?.OnboardingMeta?.onBoardingImg!!)
        //TO Do:add stuff

        recylerView.adapter=adapter
       recylerView.setLayoutManager(LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,true))
       // var layoutManager=CarouselLinearLayoutManager(context!!,true)
        var layoutManager=LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,true)
       recylerView.layoutManager=layoutManager
       var handler=Handler()
       var runnable= Runnable {   fun run()
       {
//           if(layoutManager.isSmothSCroller)
//               scrollTo(layoutManager.smothSCroller)
           recylerView.smoothScrollBy(20,0)
       }}
Shalu T D
  • 3,921
  • 2
  • 26
  • 37
  • 1
    Possible duplicate of [How to scroll the RecyclerView programatically by a specific pixels?](https://stackoverflow.com/questions/39482009/how-to-scroll-the-recyclerview-programatically-by-a-specific-pixels) – Yash Aug 11 '19 at 05:06

2 Answers2

0

LinearLayoutManager has as method scrollToPositionWithOffSet(int position, int offSet).

It can be used as:

layoutManager.scrollToPositionOffSet(10, 20)

Where position is the Adapter position of the item and offSet is pixels by which you want to scroll.

Ashish
  • 6,791
  • 3
  • 26
  • 48
Anand
  • 857
  • 1
  • 12
  • 18
0
class ImageCarouselRunnable(recyclerView: RecyclerView, layoutManager: LinearLayoutManager?, handler: Handler) : Runnable {
    var recyclerViewWeakRefrence = WeakReference(recyclerView)
    var layoutManagerWeakReference = WeakReference(layoutManager)
    var handler = WeakReference(handler)
    override fun run() {
        var mRecyclerView = recyclerViewWeakRefrence.get()
        var mLayoutManager = layoutManagerWeakReference.get()
        mRecyclerView?.scrollBy(SVConstants.SVAutoScroll.SCROLL_XOFFSET, 0)
        if (mLayoutManager?.findFirstVisibleItemPosition() == SVConstants.SVAutoScroll.VIEW_HOLDER_POSITION_4 && mLayoutManager?.findFirstVisibleItemPosition() != SVConstants.SVAutoScroll.VIEW_HOLDER_POSITION_0) {
            mRecyclerView?.layoutManager?.scrollToPosition(SVConstants.SVAutoScroll.VIEW_HOLDER_POSITION_0)// loops back to first view holder upon swiping left to right if view holder position is 4(last)
        } else if (mLayoutManager?.findFirstVisibleItemPosition() == SVConstants.SVAutoScroll.VIEW_HOLDER_POSITION_0) {
            mRecyclerView?.layoutManager?.scrollToPosition(SVConstants.SVAutoScroll.VIEW_HOLDER_POSITION_3)//loops back to second last view holder upon swiping right to left swipe if view holder position is 0
        }
        handler.get()?.postDelayed(this, SVConstants.SVAutoScroll.SCROLL_DELAY.toLong())
    }

}


private fun autoScroll() {
        if(!::handler.isInitialized){
        handler = Handler(Looper.getMainLooper())}
        mRunnable = ImageCarouselRunnable(recylerView , layoutManager, handler)
        handler.postDelayed(mRunnable,SVConstants.SVAutoScroll.SCROLL_DELAY.toLong())

    }

using scroll to position and by looping handler calls to itself I was able to achieve it