Debian Codegraph @

core/java/com/android/internal/widget/ScrollbarHelper.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.android.internal.widget;
18
19 import android.view.View;
20
21 /**
22 * A helper class to do scroll offset calculations.
23 */
24 class ScrollbarHelper {
25
26 /**
27 * @param startChild View closest to start of the list. (top or left)
28 * @param endChild View closest to end of the list (bottom or right)
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 * @param startChild View closest to start of the list. (top or left)
59 * @param endChild View closest to end of the list (bottom or right)
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 * @param startChild View closest to start of the list. (top or left)
78 * @param endChild View closest to end of the list (bottom or right)
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 // smooth scrollbar enabled. try to estimate better.
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 // estimate a size for full list.
97 return (int) ((float) laidOutArea / laidOutRange * state.getItemCount());
98 }
99 }