commit
798cf4a943
@ -0,0 +1,43 @@ |
||||
package io.legado.app.ui.widget.recycler |
||||
|
||||
import android.content.Context |
||||
import androidx.recyclerview.widget.LinearLayoutManager |
||||
import androidx.recyclerview.widget.LinearSmoothScroller |
||||
|
||||
class UpLinearLayoutManager: LinearLayoutManager { |
||||
val context: Context |
||||
|
||||
constructor(context: Context) : super(context) { |
||||
this.context = context |
||||
} |
||||
|
||||
fun smoothScrollToPosition(position: Int) { |
||||
smoothScrollToPosition(position, 0) |
||||
} |
||||
|
||||
fun smoothScrollToPosition(position: Int, offset: Int) { |
||||
val scroller = UpLinearSmoothScroller(context) |
||||
scroller.targetPosition = position |
||||
scroller.offset = offset |
||||
startSmoothScroll(scroller) |
||||
} |
||||
} |
||||
|
||||
class UpLinearSmoothScroller(context: Context?): LinearSmoothScroller(context) { |
||||
var offset = 0 |
||||
|
||||
override fun getVerticalSnapPreference(): Int { |
||||
return SNAP_TO_START |
||||
} |
||||
|
||||
override fun getHorizontalSnapPreference(): Int { |
||||
return SNAP_TO_START |
||||
} |
||||
|
||||
override fun calculateDtToFit(viewStart: Int, viewEnd: Int, boxStart: Int, boxEnd: Int, snapPreference: Int): Int { |
||||
if (snapPreference == SNAP_TO_START) { |
||||
return boxStart - viewStart + offset |
||||
} |
||||
throw IllegalArgumentException("snap preference should be SNAP_TO_START") |
||||
} |
||||
} |
Loading…
Reference in new issue