activity_main.xml
1 <android.support.v4.widget.SwipeRefreshLayout
2 android:id="@+id/sr1"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent">
5 <ListView
6 android:id="@+id/lv"
7 android:layout_width="match_parent"
8 android:layout_height="wrap_content"/>
9 </android.support.v4.widget.SwipeRefreshLayout>
MainActivity.java
1 package com.example.add;
2
3 import android.os.Handler;
4 import android.support.v4.widget.SwipeRefreshLayout;
5 import android.support.v7.app.AppCompatActivity;
6 import android.os.Bundle;
7 import android.widget.ArrayAdapter;
8 import android.widget.ListView;
9
10 import java.util.ArrayList;
11 import java.util.List;
12
13 public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
14 private SwipeRefreshLayout swipeRefreshLayout;
15 private ListView listView;
16 private List<String> list;
17 private ArrayAdapter adapter;
18
19 @Override
20 protected void onCreate(Bundle savedInstanceState) {
21 super.onCreate(savedInstanceState);
22 setContentView(R.layout.activity_main);
23 swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.sr1);
24 swipeRefreshLayout.setOnRefreshListener(this);
25 list = new ArrayList<>();
26 list.add("ssss");
27 listView = (ListView) findViewById(R.id.lv);
28 adapter = new ArrayAdapter(this
29 , android.R.layout.simple_list_item_1
30 , android.R.id.text1
31 , list);
32 listView.setAdapter(adapter);
33 }
34
35 @Override
36 public void onRefresh() {
37 new Handler().postDelayed(new Runnable() {
38 @Override
39 public void run() {
40 swipeRefreshLayout.setRefreshing(false);
41 adapter.clear();
42 list.add("1111");
43 adapter.notifyDataSetChanged();
44 }
45 },1000);
46 }
47 }