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

Source: Internet
Author: User

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

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 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 context) {super (context);} public ScrollSwipeRefreshLayout (Context context, AttributeSet attrs) {super (context, attrs);} public ViewGroup getViewGroup () {return viewGroup;} public void setViewGroup (ViewGroup 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 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
















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.