Skip to content

Commit

Permalink
update fix scrolltoPosition crash
Browse files Browse the repository at this point in the history
  • Loading branch information
sparrow007 committed May 29, 2021
1 parent 58a5f10 commit 239ead8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jackandphantom.carousellayout

import android.os.Bundle
import android.os.Handler
import androidx.appcompat.app.AppCompatActivity
import com.example.customviewimple.model.DataModel
import com.jackandphantom.carousellayout.adapter.DataAdapter
Expand All @@ -25,9 +26,12 @@ class MainActivity : AppCompatActivity() {
carouselRecyclerview.adapter = adapter
carouselRecyclerview.set3DItem(true)
carouselRecyclerview.setAlpha(true)

val carouselLayoutManager = carouselRecyclerview.getCarouselLayoutManager()
val currentlyCenterPosition = carouselRecyclerview.getSelectedPosition()

carouselLayoutManager.scrollToPosition(4)


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,12 @@ class CarouselLayoutManager constructor(
*/
override fun scrollToPosition(position: Int) {
if (position < 0 || position > itemCount - 1) return
if (!this::recycler.isInitialized || !this::state.isInitialized) {
isOrientationChange = true
selectedPosition = position
requestLayout()
return
}
mOffsetAll = calculatePositionOffset(position)
layoutItems(recycler,
state,
Expand All @@ -422,7 +428,7 @@ class CarouselLayoutManager constructor(
position: Int
) {
//Loop does not support for smooth scrolling
if (mInfinite) return
if (mInfinite||!this::recycler.isInitialized || !this::state.isInitialized) return
val finalOffset = calculatePositionOffset(position)
startScroll(mOffsetAll, finalOffset)
}
Expand Down

0 comments on commit 239ead8

Please sign in to comment.