2222 */
2323public class ScrollUtils {
2424
25+ static Method computeVerticalScrollOffsetMethod ;
26+ static Method computeVerticalScrollRangeMethod ;
27+ static Method computeVerticalScrollExtentMethod ;
28+
2529 static int computeVerticalScrollOffset (View view ) {
2630 View scrolledView = getScrolledView (view );
2731
@@ -30,9 +34,11 @@ static int computeVerticalScrollOffset(View view) {
3034 }
3135
3236 try {
33- Method method = View .class .getDeclaredMethod ("computeVerticalScrollOffset" );
34- method .setAccessible (true );
35- Object o = method .invoke (scrolledView );
37+ if (computeVerticalScrollOffsetMethod == null ){
38+ computeVerticalScrollOffsetMethod = View .class .getDeclaredMethod ("computeVerticalScrollOffset" );
39+ computeVerticalScrollOffsetMethod .setAccessible (true );
40+ }
41+ Object o = computeVerticalScrollOffsetMethod .invoke (scrolledView );
3642 if (o != null ){
3743 return (int ) o ;
3844 }
@@ -50,9 +56,11 @@ static int computeVerticalScrollRange(View view) {
5056 }
5157
5258 try {
53- Method method = View .class .getDeclaredMethod ("computeVerticalScrollRange" );
54- method .setAccessible (true );
55- Object o = method .invoke (scrolledView );
59+ if (computeVerticalScrollRangeMethod == null ) {
60+ computeVerticalScrollRangeMethod = View .class .getDeclaredMethod ("computeVerticalScrollRange" );
61+ computeVerticalScrollRangeMethod .setAccessible (true );
62+ }
63+ Object o = computeVerticalScrollRangeMethod .invoke (scrolledView );
5664 if (o != null ){
5765 return (int ) o ;
5866 }
@@ -70,9 +78,12 @@ static int computeVerticalScrollExtent(View view) {
7078 }
7179
7280 try {
73- Method method = View .class .getDeclaredMethod ("computeVerticalScrollExtent" );
74- method .setAccessible (true );
75- Object o = method .invoke (scrolledView );
81+ if (computeVerticalScrollExtentMethod == null ){
82+ computeVerticalScrollExtentMethod = View .class .getDeclaredMethod ("computeVerticalScrollExtent" );
83+ computeVerticalScrollExtentMethod .setAccessible (true );
84+ }
85+
86+ Object o = computeVerticalScrollExtentMethod .invoke (scrolledView );
7687 if (o != null ){
7788 return (int ) o ;
7889 }
0 commit comments