If you are working with Kotlin android.
Declare lateinit variable smoothScroller
lateinit var smoothScroller: SmoothScroller
Within OnCreate Mehtod Initilize the smoothScroller
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
smoothScroller = object : LinearSmoothScroller(context) {
override fun getVerticalSnapPreference(): Int {
return SNAP_TO_START
}
}
}
Finally, Check if the Adopter is Initialized or not?
Commit Old dataset changes if there are any.
Set the position.
Start scroller.
if (this@ChooseTemplate::genericAdapter.isInitialized && this@ChooseTemplate::smoothScroller.isInitialized) {
[email protected]!!.notifyDataSetChanged()
smoothScroller.setTargetPosition(
[email protected]);
dataBinding!!.rvTemplate.layoutManager!!.startSmoothScroll(
[email protected]);
}
dataBinding is an binding object.
rvTemplate is a Recycler View.
genericadapter is an adopter for rvTemplate.
templatesrowIndex is the index to check which row item is currently selected.