1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| class CustomLinearLayoutManager : RecyclerView.LayoutManager() {
override fun generateDefaultLayoutParams(): RecyclerView.LayoutParams { return RecyclerView.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT ) }
override fun isAutoMeasureEnabled(): Boolean { return true }
override fun canScrollHorizontally(): Boolean { return true }
override fun canScrollVertically(): Boolean { return false }
override fun onLayoutChildren(recycler: RecyclerView.Recycler, state: RecyclerView.State) { if (state.itemCount == 0) { removeAndRecycleAllViews(recycler) return }
var left = paddingStart var curPos = 0 if (childCount > 0) { left = getDecoratedLeft(getChildAt(0)!!) curPos = getPosition(getChildAt(0)!!) }
detachAndScrapAttachedViews(recycler)
fill(recycler, state, curPos, left, paddingStart + getAvailableSpace(), true) }
override fun scrollHorizontallyBy( dx: Int, recycler: RecyclerView.Recycler, state: RecyclerView.State ): Int { val resDx = calculateOffset(recycler, state, dx) if (resDx == 0 || itemCount == 0) { return resDx }
val d = abs(resDx) if (resDx >= 0) { val anchorView = getChildAt(0) val anchorPos = getPosition(anchorView!!) val anchorLeft = getDecoratedLeft(anchorView)
detachAndScrapAttachedViews(recycler)
fill(recycler, state, anchorPos, anchorLeft - d, getAvailableSpace() + paddingStart, true) } else { val anchorView = getChildAt(childCount - 1) val anchorPos = getPosition(anchorView!!) val anchorRight = getDecoratedRight(anchorView)
detachAndScrapAttachedViews(recycler)
fill(recycler, state, anchorPos, paddingLeft, anchorRight + d, false) }
recycler(recycler, state, resDx)
return resDx } private fun calculateOffset(recycler: RecyclerView.Recycler, state: RecyclerView.State, dx: Int): Int { if (childCount == 0 || dx == 0) { return 0 }
var fillPos = RecyclerView.NO_POSITION val d = abs(dx)
if (dx < 0) { val firstView = getChildAt(0) val firstPos = getPosition(firstView!!) val firstLeft = getDecoratedLeft(firstView)
fillPos = firstPos - 1
if (fillPos < 0 && firstLeft + d > paddingStart) { return firstLeft - paddingStart } if (firstLeft + d < paddingStart) { return dx }
} else { val lastView = getChildAt(childCount - 1) val lastPos = getPosition(lastView!!) val lastRight = getDecoratedRight(lastView)
fillPos = lastPos + 1 val endEdge = getAvailableSpace() + paddingStart
if (fillPos >= itemCount && lastRight - d < endEdge) { return lastRight - endEdge } if (lastRight - d > endEdge) { return dx } } return dx }
private fun fill(recycler: RecyclerView.Recycler, state: RecyclerView.State, anchorIndex: Int, anchorLeft: Int, anchorRight: Int, isLTR: Boolean) { var availableSpace = anchorRight - anchorLeft var fillPos = anchorIndex var left = anchorLeft var right = anchorRight val top = paddingTop
while (availableSpace > 0 && fillPos >= 0 && fillPos < state.itemCount) { val view = recycler.getViewForPosition(fillPos) if (isLTR) { addView(view) measureChildWithMargins(view, 0, 0) right = left + getDecoratedMeasuredWidth(view) val bottom = top + getDecoratedMeasuredHeight(view) layoutDecoratedWithMargins(view, left, top, right, bottom) fillPos++ left = right } else { addView(view, 0) measureChildWithMargins(view, 0, 0) left = right - getDecoratedMeasuredWidth(view) val bottom = top + getDecoratedMeasuredHeight(view) layoutDecoratedWithMargins(view, left, top, right, bottom) fillPos-- right = left }
availableSpace -= getDecoratedMeasuredWidth(view) } }
private fun recycler(recycler: RecyclerView.Recycler, state: RecyclerView.State, dx: Int) { val recycleViews = hashSetOf<View>()
if (dx > 0) { for (i in 0 until childCount) { val child = getChildAt(i)!! val right = getDecoratedRight(child) if (right > paddingStart) break recycleViews.add(child) } } if (dx < 0) { for (i in childCount - 1 downTo 0) { val child = getChildAt(i)!! val left = getDecoratedLeft(child)
if (left < getAvailableSpace() + paddingStart) break recycleViews.add(child) } }
for (view in recycleViews) { removeAndRecycleView(view, recycler) } }
private fun getAvailableSpace(): Int { return width - paddingStart - paddingEnd } }
|