Android Official auto-Pull refresh feature

Source: Internet
Author: User





Recently want to write down the refresh function, the Internet to find the third-party framework of the most famous Pulltorefresh also stopped maintenance in 2013, accidentally found that Google has already launched its own drop-down refresh function, located in the V4 package, the effect please see:




The use of the android.support.v4.widget.SwipeRefreshLayout control, the use of the method is very simple, first the need to drop the refresh of the ListView into the Swiperefreshlayout, the code is as follows:

XML layout:

<android.support  .v  4.widget   android:id= "@+id/swipe_ly"  android:layout_width=< Span class= "hljs-string" > "match_parent"  android:layout_height= "match_parent" 
      > <listview android:id= "@+id/listview"  android:l Ayout_width= "match_parent"  android:layout_height= "Ma Tch_parent " > </ListView> </android.support  .v  4.widget  ;  



Then find the control in the activity and set the listener

    ListView mListView = (ListView) findViewById(R.id.listview);    SwipeRefreshLayout mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_ly);    //设置监听    mSwipeLayout.setOnRefreshListener(this);    mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDatas);    mListView.setAdapter(mAdapter);



Then implement the method onRefresh() , in the method to do some drop-down refresh operation, it is so simple, such as the following code, in the method to send a handler analog delay operation

publicvoidonRefresh()    {        2000);    }



Send a handler message that operates after 2 seconds, and then do the following in handler:

private  Handler mhandler = new  Handler () {public  void  handlemessage  (android.os.Message msg) {switch  (ms G.what) {case  REFRESH_UI:mDatas.addAll (Ar Rays.aslist ( "Zhao Yun" , ,  "Guo Jia" )); //Refresh adapter  madapter.notifydatasetchanged (); //off refresh animation  mswipelayout.setrefreshing (false< /span>); break ; } }; };





The above is the system comes with the drop-down refresh function, code download: Click the free download code




I am a novice, welcome the Great God guidance!

Android Official auto-Pull refresh feature

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.