Merge pull request #71 from KKL369/master
重写LinearLayoutManager,修复书籍目录模糊搜索后scrollToPosition在可见范围不置顶pull/72/head^2
commit
3d4facbcb1
@ -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