1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Relativelayoutxmlns: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.SwipeRefreshLayout8 Android:id= "@+id/mpull"9 Android:layout_height= "Match_parent"Ten Android:layout_width= "Match_parent" One > A <ListView - Android:id= "@+id/mlist" - Android:layout_width= "Match_parent" the Android:layout_height= "Match_parent"> - </ListView> - - </Android.support.v4.widget.SwipeRefreshLayout> + - + </Relativelayout>
1 ImportAndroid.os.Handler;2 ImportAndroid.os.Message;3 Importandroid.support.v4.widget.SwipeRefreshLayout;4 Importandroid.support.v7.app.AppCompatActivity;5 ImportAndroid.os.Bundle;6 ImportAndroid.widget.ListView;7 ImportAndroid.widget.SimpleAdapter;8 ImportAndroid.widget.Toast;9 Ten Importjava.util.ArrayList; One ImportJava.util.HashMap; A Importjava.util.List; - ImportJava.util.Map; - the Public classPullactivityextendsappcompatactivity { - Privateswiperefreshlayout swiperefreshlayout; - PrivateListView ListView; - Privatelist List; + PrivateSimpleadapter Simpleadapter; - + @Override A protected voidonCreate (Bundle savedinstancestate) { at Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_pull); - Initview (); - setlist (); - } - in Private voidInitview () { -swiperefreshlayout=(swiperefreshlayout) Findviewbyid (r.id.mpull); toSwiperefreshlayout.setonrefreshlistener (NewSwiperefreshlayout.onrefreshlistener () { + @Override - Public voidOnrefresh () { the // * NewHandler (). postdelayed (NewThread (NewRunnable () { $ @OverridePanax Notoginseng Public voidrun () { -Swiperefreshlayout.setrefreshing (false); the + //send message after refresh AMessage msg=NewMessage (); theMsg.what=1; + mhandle.sendmessage (msg); - $ } $}), 3000); - } - }); thelistview=(ListView) Findviewbyid (r.id.mlist); - }Wuyi PrivateHandler mhandle=NewHandler () { the @Override - Public voidhandlemessage (Message msg) { Wu Super. Handlemessage (msg); - if(msg.what==1){ AboutToast.maketext (Getapplicationcontext (), "Refresh succeeded!" ", Toast.length_short). Show (); $ //data to be loaded after pull- down -List mlist=NewArrayList (); - for(inti=0;i<20;i++){ -Map map=NewHashMap (); AMap.put ("img", r.drawable.img); +Map.put ("Text", "drop-down data" +i); the mlist.add (map); - } $ List.addall (mlist); the simpleadapter.notifydatasetchanged (); the } the } the }; - Private voidsetlist () { inlist=NewArrayList (); the for(inti=0;i<20;i++){ theMap map=NewHashMap (); AboutMap.put ("img", r.drawable.img); theMap.put ("Text", "New data" +i); the list.add (map); the } +Simpleadapter=NewSimpleadapter ( This, List,r.layout.list_item,NewString[]{"img", "Text"},New int[]{r.id.my_img,r.id.my_item}]; - Listview.setadapter (simpleadapter); the }Bayi}
Android officially provides drop-down refresh controls--swiperefreshlayout