ab75d08fe55548f8105e93643f4c0f59947c2dd3.svn-base
4.22 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package com.ivt.android.me.ui.myview;
import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.ivt.android.me.R;
public class SwipeRefreshAndMoreLoadLayout extends SwipeRefreshLayout implements
OnScrollListener {
/**
* 滑动到最下面时的上拉操作
*/
private int mTounchslop;
/**
* ListView的加载中footer
*/
private View mListViewFooter;
private ListView mListView;
/**
* 按下时的y坐标
*/
private int mYdown;
private int mYlast;
private boolean isLoading = false;
private OnLoadMoreListener mOnLoadMoreListener;
private TextView mTvLoadMore;
private int mVisibleItemCount;
private int mTotalItemCount;
public SwipeRefreshAndMoreLoadLayout(Context context) {
this(context, null);
}
public SwipeRefreshAndMoreLoadLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mTounchslop = ViewConfiguration.get(context).getScaledTouchSlop();
mListViewFooter = LayoutInflater.from(context).inflate(
R.layout.myview_footer_item, null);
mTvLoadMore = (TextView) mListViewFooter.findViewById(R.id.tv_loadmore);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
super.onLayout(changed, left, top, right, bottom);
// 初始化ListView
if (mListView == null) {
getListView();
}
}
/**
* 获取ListView对象
*/
private void getListView() {
int childCount = getChildCount();
if (childCount > 0) {
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
if (child instanceof ListView) {
mListView = (ListView) child;
// 设置滚动监听器给ListView, 使得滚动的情况下也可以自动加载
mListView.setOnScrollListener(this);
}
}
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
mYdown = (int) ev.getY();
break;
case MotionEvent.ACTION_MOVE:
mYlast = (int) ev.getY();
break;
case MotionEvent.ACTION_UP:
if (canLoad()) {
loadData();
}
break;
default:
break;
}
return super.dispatchTouchEvent(ev);
}
public void setAdapte(ListView listView, ListAdapter adapter) {
if (listView != null) {
listView.addFooterView(mListViewFooter);
listView.setAdapter(adapter);
listView.removeFooterView(mListViewFooter);
}
}
private boolean canLoad() {
return !isLoading && isPullup() && isBottom();
}
private boolean enableBottomLoad() {
return !isLoading && isBottom();
}
private boolean isBottom() {
if (mListView != null && mListView.getAdapter() != null) {
return mVisibleItemCount < mTotalItemCount
&& mListView.getLastVisiblePosition() == mListView
.getAdapter().getCount() - 1;
}
return false;
}
private boolean isPullup() {
return mYdown - mYlast >= mTounchslop;
}
private void loadData() {
if (mOnLoadMoreListener != null) {
setLoading(true);
mOnLoadMoreListener.onLoadMore();
}
}
public void setLoading(boolean loading) {
isLoading = loading;
if (loading) {
mListView.addFooterView(mListViewFooter);
} else {
mListView.removeFooterView(mListViewFooter);
mYdown = 0;
mYlast = 0;
}
}
public void setLoadingContext(String string) {
mTvLoadMore.setText(string);
}
public void setLoadingContext(int resId) {
mTvLoadMore.setText(resId);
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
mVisibleItemCount = visibleItemCount;
mTotalItemCount = totalItemCount;
if (visibleItemCount < totalItemCount && enableBottomLoad()) {
loadData();
}
}
public void setOnLoadMoreListener(OnLoadMoreListener listener) {
mOnLoadMoreListener = listener;
}
public static interface OnLoadMoreListener {
void onLoadMore();
}
}