Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. class RecyclerHorizontalSnapHelper : LinearSnapHelper() {
  2. private var mVerticalHelper: OrientationHelper? = null
  3. private var mHorizontalHelper: OrientationHelper? = null
  4.  
  5.  
  6. override fun calculateDistanceToFinalSnap(@NonNull layoutManager: RecyclerView.LayoutManager, @NonNull targetView: View): IntArray {
  7. val out = IntArray(2)
  8.  
  9. if (layoutManager.canScrollHorizontally()) {
  10. out[0] = getHorizontalHelper(layoutManager)?.let { distanceToStart(targetView, it) }!!
  11. } else {
  12. out[0] = 0
  13. }
  14.  
  15. if (layoutManager.canScrollVertically()) {
  16. out[1] = getVerticalHelper(layoutManager)?.let { distanceToStart(targetView, it) }!!
  17. } else {
  18. out[1] = 0
  19. }
  20. return out
  21. }
  22.  
  23. override fun findSnapView(layoutManager: RecyclerView.LayoutManager): View? {
  24.  
  25. return if (layoutManager is LinearLayoutManager) {
  26.  
  27. if (layoutManager.canScrollHorizontally()) {
  28. getHorizontalHelper(layoutManager)?.let { getStartView(layoutManager, it) }
  29. } else {
  30. getVerticalHelper(layoutManager)?.let { getStartView(layoutManager, it) }
  31. }
  32. } else super.findSnapView(layoutManager)
  33.  
  34. }
  35.  
  36. private fun distanceToStart(targetView: View, helper: OrientationHelper): Int {
  37. return helper.getDecoratedStart(targetView) - helper.startAfterPadding
  38. }
  39.  
  40. private fun getStartView(
  41. layoutManager: RecyclerView.LayoutManager,
  42. helper: OrientationHelper
  43. ): View? {
  44.  
  45. if (layoutManager is LinearLayoutManager) {
  46. val firstChild = layoutManager.findFirstVisibleItemPosition()
  47.  
  48. val isLastItem = layoutManager
  49. .findLastCompletelyVisibleItemPosition() == layoutManager.getItemCount() - 1
  50.  
  51. if (firstChild == RecyclerView.NO_POSITION || isLastItem) {
  52. return null
  53. }
  54.  
  55. val child = layoutManager.findViewByPosition(firstChild)
  56.  
  57. return if (helper.getDecoratedEnd(child) >= helper.getDecoratedMeasurement(child) / 2 && helper.getDecoratedEnd(
  58. child
  59. ) > 0
  60. ) {
  61. child
  62. } else {
  63. if (layoutManager.findLastCompletelyVisibleItemPosition() == layoutManager.getItemCount() - 1) {
  64. null
  65. } else {
  66. layoutManager.findViewByPosition(firstChild + 1)
  67. }
  68. }
  69. }
  70.  
  71. return super.findSnapView(layoutManager)
  72. }
  73.  
  74. private fun getVerticalHelper(layoutManager: RecyclerView.LayoutManager): OrientationHelper? {
  75. if (mVerticalHelper == null) {
  76. mVerticalHelper = OrientationHelper.createVerticalHelper(layoutManager)
  77. }
  78. return mVerticalHelper
  79. }
  80.  
  81. private fun getHorizontalHelper(layoutManager: RecyclerView.LayoutManager): OrientationHelper? {
  82. if (mHorizontalHelper == null) {
  83. mHorizontalHelper = OrientationHelper.createHorizontalHelper(layoutManager)
  84. }
  85. return mHorizontalHelper
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement