Are you still using the pull-down refresh of open-source controls? You are out. Try refreshing swiperefreshlayout from the official drop-down list.

Source: Internet
Author: User

1. Preface


Pull-down refresh should be widely used. You should be familiar with it only when you are a developer or an ordinary user, especially in the time when Weibo was used, some people even click "fake Weibo" to generate a forced certificate. If some content is not pulled out, it will become uncomfortable. Alas, accidentally, Weibo's popularity has been snatched. Haha.

First, let's talk about the general implementation principle of pull-down and refresh. Many open-source controls inherit linearlayout and then hide a view on the top. They only display the following listview and listen to touch events, when the scroll to the top is still in the drop-down list, the view is displayed a little bit, and the drop-down is like this. The specific content can look at the http://blog.csdn.net/guolin_blog/article/details/9255575 this article, write well.


2. Previously popular open-source drop-down controls

Android-pulltorefresh


Android2.3 and above can all be used with support controls
  • Listview
  • Expandablelistview
  • Gridview
  • Webview
  • Scrollview
  • Horizontalscrollview
  • Viewpager

If you do not want to write it on your own in most development projects, this open-source project should be the best choice.

3. Official drop-down view
As the drop-down space of the three parties is very popular, it is easy to use to disturb the Party (Google), So Google left a drop-down control in the support package, this pull-down is also very good. Let's take a look at this effect.


Some apps in more than 4.0 of the system have seen this effect. The advantage of this control is that it does not need to occupy a large screen when loading, but only needs a dynamic color running line.

4. How to Use swiperefreshlayout
Swiperefreshlayout this class is placed in the android-support-v4.jar, this V4 general default creation of the project will help you to join, if not, you create libs in the project and then copy to enter can, if you still do not have this class in your V4 package, check whether your V4 package is up-to-date, because SRL is added in support V4 19.1.

With this class, it is easy for others.
If you want to add the drop-down XML in listview, you can simply change the parent view of listview to WRL. For example:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <android.support.v4.widget.SwipeRefreshLayout        android:id="@+id/refresh_layout"        android:layout_width="match_parent"        android:layout_height="match_parent" >        <ListView            android:id="@+id/listview"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:cacheColorHint="@android:color/transparent" >        </ListView>    </android.support.v4.widget.SwipeRefreshLayout></LinearLayout>

This is the simple XML layout.
Then add the main code:
Package COM. spring. swiperefreshlayout; import Java. util. arraylist; import Java. util. list; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. support. v4.widget. swiperefreshlayout; import android. support. v4.widget. swiperefreshlayout. onrefreshlistener; import android. widget. arrayadapter; import android. widget. listview; /*** pull-down update of the common list * @ author YD **/public class listviewactivity extends activity implements onrefreshlistener {private list <string> datas = new arraylist <string> (); // Lis data private listview = NULL; private swiperefreshlayout refresh_layout = NULL; // refresh control private arrayadapter <string> adapter; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. listview_layout); listview = (listview) This. findviewbyid (R. id. listview); refresh_layout = (swiperefreshlayout) This. findviewbyid (R. id. refresh_layout); refresh_layout.setcolorscheme (R. color. green, R. color. gray, R. color. blue_50, R. color. light_white); // set the running color value adapter = new arrayadapter <string> (this, android. r. layout. simple_list_item_1, android. r. id. text1, datas); listview. setadapter (adapter); For (INT I = 0; I <30; I ++) {datas. add ("item:" + I);} adapter. notifydatasetchanged (); refresh_layout.setonrefreshlistener (this); // set the drop-down listener} @ overridepublic void onrefresh () {New thread (New runnable () {// The function triggered by the drop-down menu, who is here 1 s and then add a data, then update the interface @ overridepublic void run () {try {thread. sleep (1000);} catch (interruptedexception e) {e. printstacktrace ();} datas. add (0, "item: refresh... "); handler. sendemptymessage (0 );}}). start ();} private myhandler handler = new myhandler (); Class myhandler extends handler {@ overridepublic void handlemessage (Message MSG) {Switch (MSG. what) {Case 0: refresh_layout.setrefreshing (false); adapter. yydatasetchanged (); break; default: Break ;}}}}

A few lines of code can achieve such a nice drop-down view. Do you think this control is simple and easy to use.
5. drop-down conflicts between webview and swiperefreshlayout
Because webview is special, if special processing is not performed, the scroll of the drop-down and webview will conflict. This solution is also very simple. Just rewrite the swiperefreshlayout OntoucheventMethod.
Code:
Package COM. spring. swiperefreshlayout. view; import android. content. context; import android. support. v4.widget. swiperefreshlayout; import android. util. attributeset; import android. view. motionevent; import android. view. viewgroup;/*** rewrite the ontouchevent to prevent conflict between the rolling events of webview and the events of the pull-down update, when the webview is rolled to the top, a pull-down refresh * @ author YD **/public class scrollswiperefreshlayout extends swiperefreshlayout {private viewgroup viewg Roup; Public scrollswiperefreshlayout (context) {super (context);} public scrollswiperefreshlayout (context, attributeset attrs) {super (context, attrs);} public viewgroup getviewgroup () {return viewgroup;} public void setviewgroup (viewgroup) {This. viewgroup = viewgroup;} @ overridepublic Boolean ontouchevent (motionevent arg0) {If (null! = Viewgroup) {If (viewgroup. getscrolly ()> 1) {// directly truncates the time to spread return false;} else {return Super. ontouchevent (arg0) ;}} return Super. ontouchevent (arg0 );}}

The use of the code in main is no different from the previous one, but you just need to set a subview for it. Eg:
Package COM. spring. swiperefreshlayout; import android. app. activity; import android. OS. bundle; import android. support. v4.widget. swiperefreshlayout. onrefreshlistener; import android. webKit. webchromeclient; import android. webKit. webview; import android. webKit. webviewclient; import COM. spring. swiperefreshlayout. view. callback;/*** pull-down update of the browser ** @ author YD **/public class webviewactivity extends activity implements onrefreshlistener {private webview = NULL; private scrollswiperefreshlayout refreshlayout = NULL; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. webview_layout); webview = (webview) This. findviewbyid (R. id. webview); refreshlayout = (scrollswiperefreshlayout) This. findviewbyid (R. id. refresh_layout); refreshlayout. setviewgroup (webview); // sets the subviewrefreshlayout that listens for scrolling. setonrefreshlistener (this); // set the color refreshlayout. setcolorscheme (R. color. green, R. color. gray, R. color. blue_50, R. color. light_white); webview. loadurl ("http://blog.csdn.net/spring_he/article/details/19359099"); webview. setwebchromeclient (New webchromeclient () {@ overridepublic void onprogresschanged (webview view, int newprogress) {super. onprogresschanged (view, newprogress); If (newprogress = 100) {// set the animation refreshlayout to end after loading. setrefreshing (false) ;}}}); webview. setwebviewclient (New webviewclient () {@ overridepublic Boolean shouldoverrideurlloading (webview view, string URL) {view. loadurl (URL); Return true ;}}) ;}@ overridepublic void onrefresh () {// reload webview from the drop-down list. reload ();}}

All of them are on the top, so I will not post them here.
Source code: https://github.com/hexiaochun/SwipeRefreshLayout













Are you still using the pull-down refresh of open-source controls? You are out. Try refreshing swiperefreshlayout from the official drop-down list.

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.