-
Notifications
You must be signed in to change notification settings - Fork 0
/
android自带的下拉刷新
61 lines (56 loc) · 2.07 KB
/
android自带的下拉刷新
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
1.layout.xml中代码:
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/swipe_container"
tools:context="com.dhcc.simple.MainActivity">
<ListView
android:id="@+id/listview"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</android.support.v4.widget.SwipeRefreshLayout>
2.java code:
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
private SwipeRefreshLayout mSwipeRefreshLayout;
private ListView mListView;
private List<String> mData ;
private ArrayAdapter<String> mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
mListView = (ListView) findViewById(R.id.listview);
if (mAdapter == null){
mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mData);
}
mListView.setAdapter(mAdapter);
mSwipeRefreshLayout.setOnRefreshListener(this);
mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
android.R.color.holo_green_light, android.R.color.holo_orange_light,
android.R.color.holo_red_light);
}
private void initData() {
if (mData == null) {
mData = new ArrayList<String>();
for (int i = 0;i < 10;i++){
mData.add("item" + i);
}
}
}
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mSwipeRefreshLayout.setRefreshing(false);
}
},2000);
}
}