-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathCustomExampleActivity.java
More file actions
216 lines (181 loc) · 7.71 KB
/
CustomExampleActivity.java
File metadata and controls
216 lines (181 loc) · 7.71 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package com.scwang.refreshlayout.activity.example;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Space;
import android.widget.TextView;
import com.scwang.refreshlayout.R;
import com.scwang.refreshlayout.adapter.BaseRecyclerAdapter;
import com.scwang.refreshlayout.adapter.SmartViewHolder;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.internal.ArrowDrawable;
import com.scwang.smartrefresh.layout.internal.ProgressDrawable;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.util.SmartUtil;
import java.util.Arrays;
import java.util.Collection;
import static android.R.layout.simple_list_item_2;
/**
* 自定义Header功能使用
*/
public class CustomExampleActivity extends AppCompatActivity {
private BaseRecyclerAdapter<Void> mAdapter;
private static boolean isFirstEnter = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example_custom);
final Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
ListView listView = findViewById(R.id.listView);
listView.setAdapter(mAdapter = new BaseRecyclerAdapter<Void>(simple_list_item_2) {
@Override
protected void onBindViewHolder(SmartViewHolder holder, Void model, int position) {
holder.text(android.R.id.text1, getString(R.string.item_example_number_title, position));
holder.text(android.R.id.text2, getString(R.string.item_example_number_abstract, position));
holder.textColorId(android.R.id.text2, R.color.colorTextAssistant);
}
});
final RefreshLayout refreshLayout = findViewById(R.id.refreshLayout);
refreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
refreshLayout.getLayout().postDelayed(new Runnable() {
@Override
public void run() {
mAdapter.refresh(initData());
refreshLayout.finishRefresh();
}
}, 2000);
}
});
refreshLayout.setRefreshHeader(new ClassicsHeader(this));
refreshLayout.setHeaderHeight(60);
//触发自动刷新
if (isFirstEnter) {
isFirstEnter = false;
refreshLayout.autoRefresh();
} else {
mAdapter.refresh(initData());
}
}
private Collection<Void> initData() {
return Arrays.asList(null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null);
}
public static class ClassicsHeader extends LinearLayout implements RefreshHeader {
private TextView mHeaderText;//标题文本
private ImageView mArrowView;//下拉箭头
private ImageView mProgressView;//刷新动画视图
private ProgressDrawable mProgressDrawable;//刷新动画
public ClassicsHeader(Context context) {
this(context, null);
}
public ClassicsHeader(Context context, AttributeSet attrs) {
super(context, attrs, 0);
setGravity(Gravity.CENTER);
mHeaderText = new TextView(context);
mProgressDrawable = new ProgressDrawable();
mArrowView = new ImageView(context);
mProgressView = new ImageView(context);
mProgressView.setImageDrawable(mProgressDrawable);
mArrowView.setImageDrawable(new ArrowDrawable());
addView(mProgressView, SmartUtil.dp2px(20), SmartUtil.dp2px(20));
addView(mArrowView, SmartUtil.dp2px(20), SmartUtil.dp2px(20));
addView(new Space(context), SmartUtil.dp2px(20), SmartUtil.dp2px(20));
addView(mHeaderText, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
setMinimumHeight(SmartUtil.dp2px(60));
}
@NonNull
public View getView() {
return this;//真实的视图就是自己,不能返回null
}
@NonNull
@Override
public SpinnerStyle getSpinnerStyle() {
return SpinnerStyle.Translate;//指定为平移,不能null
}
@Override
public void onStartAnimator(@NonNull RefreshLayout layout, int height, int maxDragHeight) {
mProgressDrawable.start();//开始动画
}
@Override
public int onFinish(@NonNull RefreshLayout layout, boolean success) {
mProgressDrawable.stop();//停止动画
mProgressView.setVisibility(GONE);//隐藏动画
if (success){
mHeaderText.setText("刷新完成");
} else {
mHeaderText.setText("刷新失败");
}
return 500;//延迟500毫秒之后再弹回
}
@Override
public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
switch (newState) {
case None:
case PullDownToRefresh:
mHeaderText.setText("下拉开始刷新");
mArrowView.setVisibility(VISIBLE);//显示下拉箭头
mProgressView.setVisibility(GONE);//隐藏动画
mArrowView.animate().rotation(0);//还原箭头方向
break;
case Refreshing:
mHeaderText.setText("正在刷新");
mProgressView.setVisibility(VISIBLE);//显示加载动画
mArrowView.setVisibility(GONE);//隐藏箭头
break;
case ReleaseToRefresh:
mHeaderText.setText("释放立即刷新");
mArrowView.animate().rotation(180);//显示箭头改为朝上
break;
}
}
@Override
public void setPrimaryColors(int... colors) {
}
@Override
public void onAutoRefresh(boolean isAutoRefresh) {
}
@Override
public void onInitialized(@NonNull RefreshKernel kernel, int height, int maxDragHeight) {
}
@Override
public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {
}
// @Override
// public void onPulling(float percent, int offset, int height, int maxDragHeight) {
//
// }
// @Override
// public void onReleasing(float percent, int offset, int height, int maxDragHeight) {
//
// }
@Override
public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {
}
@Override
public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {
}
@Override
public boolean isSupportHorizontalDrag() {
return false;
}
}
}