| 1 | /* |
| 2 | * This is the source code of Telegram for Android v. 5.x.x. |
| 3 | * It is licensed under GNU GPL v. 2 or later. |
| 4 | * You should have received a copy of the license in this archive (see LICENSE). |
| 5 | * |
| 6 | * Copyright Nikolai Kudashov, 2013-2018. |
| 7 | */ |
| 8 | package androidx.recyclerview.widget; |
| 9 | |
| 10 | import android.content.Context; |
| 11 | import android.graphics.Rect; |
| 12 | import android.view.View; |
| 13 | |
| 14 | |
| 15 | |
| 16 | import org.telegram.ui.Cells.ChatMessageCell; |
| 17 | |
| 18 | import java.util.ArrayList; |
| 19 | import java.util.Arrays; |
| 20 | |
| 21 | /** |
| 22 | * A {@link RecyclerView.LayoutManager} implementations that lays out items in a grid. |
| 23 | * <p> |
| 24 | * By default, each item occupies 1 span. You can change it by providing a custom |
| 25 | * {@link SpanSizeLookup} instance via {@link #setSpanSizeLookup(SpanSizeLookup)}. |
| 26 | */ |
| 27 | public class GridLayoutManagerFixed extends GridLayoutManager { |
| 28 | |
| 29 | private ArrayList<View> additionalViews = new ArrayList<>(4); |
| 30 | private boolean canScrollVertically = true; |
| 31 | |
| 32 | public GridLayoutManagerFixed(Context context, int spanCount) { |
| 33 | super(context, spanCount); |
| 34 | } |
| 35 | |
| 36 | public GridLayoutManagerFixed(Context context, int spanCount, int orientation, boolean reverseLayout) { |
| 37 | super(context, spanCount, orientation, reverseLayout); |
| 38 | } |
| 39 | |
| 40 | protected boolean hasSiblingChild(int position) { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | public void setCanScrollVertically(boolean value) { |
| 45 | canScrollVertically = value; |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public boolean canScrollVertically() { |
| 50 | return canScrollVertically; |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | protected void recycleViewsFromStart(RecyclerView.Recycler recycler, int scrollingOffset, |
| 55 | int noRecycleSpace) { |
| 56 | if (scrollingOffset < 0) { |
| 57 | return; |
| 58 | } |
| 59 | // ignore padding, ViewGroup may not clip children. |
| 60 | final int childCount = getChildCount(); |
| 61 | if (mShouldReverseLayout) { |
| 62 | for (int i = childCount - 1; i >= 0; i--) { |
| 63 | View child = getChildAt(i); |
| 64 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); |
| 65 | if (child.getBottom() + params.bottomMargin > scrollingOffset |
| 66 | || child.getTop() + child.getHeight() > scrollingOffset) { |
| 67 | // stop here |
| 68 | recycleChildren(recycler, childCount - 1, i); |
| 69 | return; |
| 70 | } |
| 71 | } |
| 72 | } else { |
| 73 | for (int i = 0; i < childCount; i++) { |
| 74 | View child = getChildAt(i); |
| 75 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); |
| 76 | if (child.getBottom() + params.bottomMargin > scrollingOffset |
| 77 | || child.getTop() + child.getHeight() > scrollingOffset) { |
| 78 | // stop here |
| 79 | recycleChildren(recycler, 0, i); |
| 80 | return; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | protected int[] calculateItemBorders(int[] cachedBorders, int spanCount, int totalSpace) { |
| 88 | if (cachedBorders == null || cachedBorders.length != spanCount + 1 || cachedBorders[cachedBorders.length - 1] != totalSpace) { |
| 89 | cachedBorders = new int[spanCount + 1]; |
| 90 | } |
| 91 | cachedBorders[0] = 0; |
| 92 | for (int i = 1; i <= spanCount; i++) { |
| 93 | cachedBorders[i] = (int) Math.ceil(i / (float) spanCount * totalSpace); |
| 94 | } |
| 95 | return cachedBorders; |
| 96 | } |
| 97 | |
| 98 | public boolean shouldLayoutChildFromOpositeSide(View child) { |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | protected void measureChild(View view, int otherDirParentSpecMode, boolean alreadyMeasured) { |
| 104 | final LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 105 | final Rect decorInsets = lp.mDecorInsets; |
| 106 | final int verticalInsets = decorInsets.top + decorInsets.bottom |
| 107 | + lp.topMargin + lp.bottomMargin; |
| 108 | final int horizontalInsets = decorInsets.left + decorInsets.right |
| 109 | + lp.leftMargin + lp.rightMargin; |
| 110 | final int availableSpaceInOther = mCachedBorders[lp.mSpanSize]; |
| 111 | final int wSpec = getChildMeasureSpec(availableSpaceInOther, otherDirParentSpecMode, |
| 112 | horizontalInsets, lp.width, false); |
| 113 | final int hSpec = getChildMeasureSpec(mOrientationHelper.getTotalSpace(), getHeightMode(), |
| 114 | verticalInsets, lp.height, true); |
| 115 | measureChildWithDecorationsAndMargin(view, wSpec, hSpec, alreadyMeasured); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | void layoutChunk(RecyclerView.Recycler recycler, RecyclerView.State state, LayoutState layoutState, LayoutChunkResult result) { |
| 120 | final int otherDirSpecMode = mOrientationHelper.getModeInOther(); |
| 121 | |
| 122 | final boolean layingOutInPrimaryDirection = layoutState.mItemDirection == LayoutState.ITEM_DIRECTION_TAIL; |
| 123 | result.mConsumed = 0; |
| 124 | |
| 125 | int startPosition = layoutState.mCurrentPosition; |
| 126 | if ( |
| 127 | mShouldReverseLayout && |
| 128 | layoutState.mLayoutDirection != LayoutState.LAYOUT_START && |
| 129 | hasSiblingChild(layoutState.mCurrentPosition) && |
| 130 | findViewByPosition(layoutState.mCurrentPosition + 1) == null |
| 131 | ) { |
| 132 | if (hasSiblingChild(layoutState.mCurrentPosition + 1)) { |
| 133 | layoutState.mCurrentPosition += 3; |
| 134 | } else { |
| 135 | layoutState.mCurrentPosition += 2; |
| 136 | } |
| 137 | int backupPosition = layoutState.mCurrentPosition; |
| 138 | for (int a = layoutState.mCurrentPosition; a > startPosition; a--) { |
| 139 | View view = layoutState.next(recycler); |
| 140 | if (view == null) { |
| 141 | continue; |
| 142 | } |
| 143 | additionalViews.add(view); |
| 144 | if (a != backupPosition) { |
| 145 | calculateItemDecorationsForChild(view, mDecorInsets); |
| 146 | measureChild(view, otherDirSpecMode, false); |
| 147 | int size = mOrientationHelper.getDecoratedMeasurement(view); |
| 148 | layoutState.mOffset -= size; |
| 149 | layoutState.mAvailable += size; |
| 150 | } |
| 151 | } |
| 152 | layoutState.mCurrentPosition = backupPosition; |
| 153 | } |
| 154 | |
| 155 | boolean working = true; |
| 156 | while (working) { |
| 157 | int count = 0; |
| 158 | int remainingSpan = mSpanCount; |
| 159 | working = !additionalViews.isEmpty(); |
| 160 | while (count < mSpanCount && layoutState.hasMore(state) && remainingSpan > 0) { |
| 161 | int pos = layoutState.mCurrentPosition; |
| 162 | final int spanSize = getSpanSize(recycler, state, pos); |
| 163 | remainingSpan -= spanSize; |
| 164 | if (remainingSpan < 0) { |
| 165 | break; |
| 166 | } |
| 167 | View view; |
| 168 | if (!additionalViews.isEmpty()) { |
| 169 | view = additionalViews.get(0); |
| 170 | additionalViews.remove(0); |
| 171 | layoutState.mCurrentPosition--; |
| 172 | } else { |
| 173 | view = layoutState.next(recycler); |
| 174 | } |
| 175 | if (view == null) { |
| 176 | break; |
| 177 | } |
| 178 | mSet[count] = view; |
| 179 | count++; |
| 180 | if (layoutState.mLayoutDirection == LayoutState.LAYOUT_START && remainingSpan <= 0 && hasSiblingChild(pos)) { |
| 181 | working = true; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | if (count == 0) { |
| 186 | result.mFinished = true; |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | int maxSize = 0; |
| 191 | float maxSizeInOther = 0; |
| 192 | |
| 193 | assignSpans(recycler, state, count, layingOutInPrimaryDirection); |
| 194 | for (int i = 0; i < count; i++) { |
| 195 | View view = mSet[i]; |
| 196 | if (layoutState.mScrapList == null) { |
| 197 | if (layingOutInPrimaryDirection) { |
| 198 | addView(view); |
| 199 | } else { |
| 200 | addView(view, 0); |
| 201 | } |
| 202 | } else { |
| 203 | if (layingOutInPrimaryDirection) { |
| 204 | addDisappearingView(view); |
| 205 | } else { |
| 206 | addDisappearingView(view, 0); |
| 207 | } |
| 208 | } |
| 209 | calculateItemDecorationsForChild(view, mDecorInsets); |
| 210 | |
| 211 | measureChild(view, otherDirSpecMode, false); |
| 212 | final int size = mOrientationHelper.getDecoratedMeasurement(view); |
| 213 | if (size > maxSize) { |
| 214 | maxSize = size; |
| 215 | } |
| 216 | final LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 217 | final float otherSize = 1f * mOrientationHelper.getDecoratedMeasurementInOther(view) / lp.mSpanSize; |
| 218 | if (otherSize > maxSizeInOther) { |
| 219 | maxSizeInOther = otherSize; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // Views that did not measure the maxSize has to be re-measured |
| 224 | // We will stop doing this once we introduce Gravity in the GLM layout params |
| 225 | for (int i = 0; i < count; i++) { |
| 226 | final View view = mSet[i]; |
| 227 | if (mOrientationHelper.getDecoratedMeasurement(view) != maxSize) { |
| 228 | final LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 229 | final Rect decorInsets = lp.mDecorInsets; |
| 230 | final int verticalInsets = decorInsets.top + decorInsets.bottom + lp.topMargin + lp.bottomMargin; |
| 231 | final int horizontalInsets = decorInsets.left + decorInsets.right + lp.leftMargin + lp.rightMargin; |
| 232 | final int totalSpaceInOther = mCachedBorders[lp.mSpanSize]; |
| 233 | |
| 234 | final int wSpec = getChildMeasureSpec(totalSpaceInOther, View.MeasureSpec.EXACTLY, horizontalInsets, lp.width, false); |
| 235 | final int hSpec = View.MeasureSpec.makeMeasureSpec(maxSize - verticalInsets, View.MeasureSpec.EXACTLY); |
| 236 | |
| 237 | measureChildWithDecorationsAndMargin(view, wSpec, hSpec, true); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | int left, right, top, bottom; |
| 242 | boolean fromOppositeSide = shouldLayoutChildFromOpositeSide(mSet[0]); |
| 243 | if ( |
| 244 | fromOppositeSide && layoutState.mLayoutDirection == LayoutState.LAYOUT_START || |
| 245 | !fromOppositeSide && layoutState.mLayoutDirection == LayoutState.LAYOUT_END |
| 246 | ) { |
| 247 | if (layoutState.mLayoutDirection == LayoutState.LAYOUT_START) { |
| 248 | bottom = layoutState.mOffset - result.mConsumed; |
| 249 | top = bottom - maxSize; |
| 250 | left = 0; |
| 251 | } else { |
| 252 | top = layoutState.mOffset + result.mConsumed; |
| 253 | bottom = top + maxSize; |
| 254 | left = getWidth(); |
| 255 | } |
| 256 | for (int i = count - 1; i >= 0; i--) { |
| 257 | View view = mSet[i]; |
| 258 | LayoutParams params = (LayoutParams) view.getLayoutParams(); |
| 259 | |
| 260 | right = mOrientationHelper.getDecoratedMeasurementInOther(view); |
| 261 | if (layoutState.mLayoutDirection == LayoutState.LAYOUT_END) { |
| 262 | left -= right; |
| 263 | } |
| 264 | layoutDecoratedWithMargins(view, left, top, left + right, bottom); |
| 265 | if (layoutState.mLayoutDirection == LayoutState.LAYOUT_START) { |
| 266 | left += right; |
| 267 | } |
| 268 | if (params.isItemRemoved() || params.isItemChanged()) { |
| 269 | result.mIgnoreConsumed = true; |
| 270 | } |
| 271 | result.mFocusable |= view.hasFocusable(); |
| 272 | } |
| 273 | } else { |
| 274 | if (layoutState.mLayoutDirection == LayoutState.LAYOUT_START) { |
| 275 | bottom = layoutState.mOffset - result.mConsumed; |
| 276 | top = bottom - maxSize; |
| 277 | left = getWidth(); |
| 278 | } else { |
| 279 | top = layoutState.mOffset + result.mConsumed; |
| 280 | bottom = top + maxSize; |
| 281 | left = 0; |
| 282 | } |
| 283 | for (int i = 0; i < count; i++) { |
| 284 | View view = mSet[i]; |
| 285 | LayoutParams params = (LayoutParams) view.getLayoutParams(); |
| 286 | |
| 287 | right = mOrientationHelper.getDecoratedMeasurementInOther(view); |
| 288 | if (layoutState.mLayoutDirection == LayoutState.LAYOUT_START) { |
| 289 | left -= right; |
| 290 | } |
| 291 | layoutDecoratedWithMargins(view, left, top, left + right, bottom); |
| 292 | if (layoutState.mLayoutDirection == LayoutState.LAYOUT_END) { |
| 293 | left += right; |
| 294 | } |
| 295 | if (params.isItemRemoved() || params.isItemChanged()) { |
| 296 | result.mIgnoreConsumed = true; |
| 297 | } |
| 298 | result.mFocusable |= view.hasFocusable(); |
| 299 | } |
| 300 | } |
| 301 | result.mConsumed += maxSize; |
| 302 | Arrays.fill(mSet, null); |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | |