1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context="com.example.administrator.mydemo.PullActivity"> 7 <android.support.v4.widget.SwipeRefreshLayout 8 android:id="@+id/mpull" 9 android:layout_height="match_parent" 10 android:layout_width="match_parent" 11 > 12 <ListView 13 android:id="@+id/mlist" 14 android:layout_width="match_parent" 15 android:layout_height="match_parent"> 16 </ListView> 17 18 </android.support.v4.widget.SwipeRefreshLayout> 19 20 21 </RelativeLayout>
1 import android.os.Handler; 2 import android.os.Message; 3 import android.support.v4.widget.SwipeRefreshLayout; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.widget.ListView; 7 import android.widget.SimpleAdapter; 8 import android.widget.Toast; 9 10 import java.util.ArrayList; 11 import java.util.HashMap; 12 import java.util.List; 13 import java.util.Map; 14 15 public class PullActivity extends AppCompatActivity { 16 private SwipeRefreshLayout swipeRefreshLayout; 17 private ListView listView; 18 private List list; 19 private SimpleAdapter simpleAdapter; 20 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.activity_pull); 25 initView(); 26 setList(); 27 } 28 29 private void initView(){ 30 swipeRefreshLayout= (SwipeRefreshLayout) findViewById(R.id.mpull); 31 swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 32 @Override 33 public void onRefresh() { 34 // 35 new Handler().postDelayed(new Thread(new Runnable() { 36 @Override 37 public void run() { 38 swipeRefreshLayout.setRefreshing(false); 39 40 //刷新之后发送message 41 Message msg=new Message(); 42 msg.what=1; 43 mhandle.sendMessage(msg); 44 45 } 46 }),3000); 47 } 48 }); 49 listView= (ListView) findViewById(R.id.mlist); 50 } 51 private Handler mhandle=new Handler(){ 52 @Override 53 public void handleMessage(Message msg) { 54 super.handleMessage(msg); 55 if (msg.what==1){ 56 Toast.makeText(getApplicationContext(),"刷新成功!",Toast.LENGTH_SHORT).show(); 57 //下拉之后需要加载的数据 58 List mlist=new ArrayList(); 59 for (int i=0;i<20;i++){ 60 Map map=new HashMap(); 61 map.put("img",R.drawable.img); 62 map.put("text","下拉数据"+i); 63 mlist.add(map); 64 } 65 list.addAll(mlist); 66 simpleAdapter.notifyDataSetChanged(); 67 } 68 } 69 }; 70 private void setList(){ 71 list=new ArrayList(); 72 for (int i=0;i<20;i++){ 73 Map map=new HashMap(); 74 map.put("img",R.drawable.img); 75 map.put("text","新数据"+i); 76 list.add(map); 77 } 78 simpleAdapter=new SimpleAdapter(this,list,R.layout.list_item,new String[]{"img","text"},new int[]{R.id.my_img,R.id.my_item}); 79 listView.setAdapter(simpleAdapter); 80 } 81 }