/
ScrollStateUtil.java
124 lines (109 loc) · 3.31 KB
/
ScrollStateUtil.java
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
package com.sxu.baselibrary.commonutils;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ScrollView;
/*******************************************************************************
* Description: 检查View的滑动位置
*
* Author: Freeman
*
* Date: 2017/12/26
*
* Copyright: all rights reserved by Freeman.
*******************************************************************************/
public class ScrollStateUtil {
private ScrollStateUtil() {
}
/**
* 是否滑动到顶部
* @param view
* @return
*/
public static boolean reachTop(View view) {
if (view instanceof AbsListView) {
return absListViewReachTop((AbsListView) view);
} else if (view instanceof ScrollView) {
return scrollViewReachTop((ScrollView) view);
} else if (view instanceof RecyclerView) {
return !view.canScrollVertically(-1);
} else {
return true;
}
}
/**
* 是否滑动到底部
* @param view
* @return
*/
public static boolean reachBottom(View view) {
if (view instanceof AbsListView) {
return absListViewReachBottom((AbsListView) view);
} else if (view instanceof RecyclerView) {
return !view.canScrollVertically(1);
} else {
return false;
}
}
/**
* AbsListView类型的View是否已滑动到顶部
* @param listView
* @return
*/
public static boolean absListViewReachTop(AbsListView listView) {
int firstItemTop = 0;
if (listView.getChildCount() > 0) {
firstItemTop = listView.getChildAt(0).getTop() - listView.getPaddingTop();
}
return listView.getFirstVisiblePosition() == 0 && firstItemTop == 0;
}
/**
* ScrollView是否已滑动到顶部
* @param scrollView
* @return
*/
public static boolean scrollViewReachTop(ScrollView scrollView) {
return scrollView.getScrollY() == 0;
}
/**
* RecycleView是否已滑动到顶部
* @param recyclerView
* @return
*/
public static boolean recycleViewReachTop(RecyclerView recyclerView) {
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
if (layoutManager == null || layoutManager.getItemCount() == 0) {
return true;
}
View firstChild = recyclerView.getChildAt(0);
int position = recyclerView.getChildAdapterPosition(firstChild);
int firstItemTop = firstChild.getTop() - recyclerView.getPaddingTop();
if (layoutManager instanceof LinearLayoutManager
&& ((LinearLayoutManager) layoutManager).getOrientation() == LinearLayoutManager.VERTICAL) {
return position == 0 && firstItemTop == 0;
} else if (layoutManager instanceof GridLayoutManager) {
return position < ((GridLayoutManager)layoutManager).getSpanCount() && firstItemTop == 0;
} else {
/**
* Nothing
*/
}
return false;
}
/**
* AbsListView类型的View是否已滑动到底部
* @param listView
* @return
*/
public static boolean absListViewReachBottom(AbsListView listView) {
if (listView.getChildCount() > 0) {
int lastItemBottom = listView.getChildAt(listView.getChildCount() - 1).getBottom();
int listHeight = listView.getBottom() - listView.getTop();
return listView.getLastVisiblePosition() == listView.getAdapter().getCount() - 1
&& lastItemBottom <= listHeight;
}
return false;
}
}