| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.widget; |
| 18 |
|
| 19 |
import android.view.View; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
class ScrollbarHelper { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
static int computeScrollOffset(RecyclerView.State state, OrientationHelper orientation, |
| 31 |
View startChild, View endChild, RecyclerView.LayoutManager lm, |
| 32 |
boolean smoothScrollbarEnabled, boolean reverseLayout) { |
| 33 |
if (lm.getChildCount() == 0 || state.getItemCount() == 0 || startChild == null |
| 34 |
|| endChild == null) { |
| 35 |
return 0; |
| 36 |
} |
| 37 |
final int minPosition = Math.min(lm.getPosition(startChild), |
| 38 |
lm.getPosition(endChild)); |
| 39 |
final int maxPosition = Math.max(lm.getPosition(startChild), |
| 40 |
lm.getPosition(endChild)); |
| 41 |
final int itemsBefore = reverseLayout |
| 42 |
? Math.max(0, state.getItemCount() - maxPosition - 1) |
| 43 |
: Math.max(0, minPosition); |
| 44 |
if (!smoothScrollbarEnabled) { |
| 45 |
return itemsBefore; |
| 46 |
} |
| 47 |
final int laidOutArea = Math.abs(orientation.getDecoratedEnd(endChild) |
| 48 |
- orientation.getDecoratedStart(startChild)); |
| 49 |
final int itemRange = Math.abs(lm.getPosition(startChild) |
| 50 |
- lm.getPosition(endChild)) + 1; |
| 51 |
final float avgSizePerRow = (float) laidOutArea / itemRange; |
| 52 |
|
| 53 |
return Math.round(itemsBefore * avgSizePerRow + (orientation.getStartAfterPadding() |
| 54 |
- orientation.getDecoratedStart(startChild))); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
static int computeScrollExtent(RecyclerView.State state, OrientationHelper orientation, |
| 62 |
View startChild, View endChild, RecyclerView.LayoutManager lm, |
| 63 |
boolean smoothScrollbarEnabled) { |
| 64 |
if (lm.getChildCount() == 0 || state.getItemCount() == 0 || startChild == null |
| 65 |
|| endChild == null) { |
| 66 |
return 0; |
| 67 |
} |
| 68 |
if (!smoothScrollbarEnabled) { |
| 69 |
return Math.abs(lm.getPosition(startChild) - lm.getPosition(endChild)) + 1; |
| 70 |
} |
| 71 |
final int extend = orientation.getDecoratedEnd(endChild) |
| 72 |
- orientation.getDecoratedStart(startChild); |
| 73 |
return Math.min(orientation.getTotalSpace(), extend); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
static int computeScrollRange(RecyclerView.State state, OrientationHelper orientation, |
| 81 |
View startChild, View endChild, RecyclerView.LayoutManager lm, |
| 82 |
boolean smoothScrollbarEnabled) { |
| 83 |
if (lm.getChildCount() == 0 || state.getItemCount() == 0 || startChild == null |
| 84 |
|| endChild == null) { |
| 85 |
return 0; |
| 86 |
} |
| 87 |
if (!smoothScrollbarEnabled) { |
| 88 |
return state.getItemCount(); |
| 89 |
} |
| 90 |
|
| 91 |
final int laidOutArea = orientation.getDecoratedEnd(endChild) |
| 92 |
- orientation.getDecoratedStart(startChild); |
| 93 |
final int laidOutRange = Math.abs(lm.getPosition(startChild) |
| 94 |
- lm.getPosition(endChild)) |
| 95 |
+ 1; |
| 96 |
|
| 97 |
return (int) ((float) laidOutArea / laidOutRange * state.getItemCount()); |
| 98 |
} |
| 99 |
} |