Code implementation Android5.0 drop-down refresh effect

Source: Internet
Author: User

To implement a drop-down refresh similar to Gmail.

Project address:Https://github.com/stormzhang/SwipeRefreshLayoutDemo

One, defined in the XML file

This control is provided in SupportV4, called Swiperefreshlayout. This view is actually a parent control that we can define as follows.

<Android.support.v4.widget.SwipeRefreshLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/swipe_container"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent" >    <ListViewAndroid:id= "@+id/listview"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:padding= "16DP" /></Android.support.v4.widget.SwipeRefreshLayout>

There is no feeling it and the ListView a dime relationship is not! It's so convenient, it's much simpler than the previous ListView drop-down refresh. As soon as the user pulls down in the range of the control, a pull-down round ball will appear automatically. In practical use, we still need to do simple processing in Java code.

Second, through the Java code to set up

 mswipelayout = (Swiperefreshlayout) Findviewbyid (R.id.swipe_container); Mswipelayout.setonrefreshlistener ( this         );  //  set the color on the drop-down circle, blue, green, orange, red   Mswipelayout.setcolorschemeresources (Android. R.color.holo_blue_bright, Android. R.color.holo_green_light, Android. R.color.holo_orange_light, Android.        R.color.holo_red_light); Mswipelayout.setdistancetotriggersync ( 400); //  setting how much distance the finger pulls down on the screen triggers a drop-down refresh  mswipelayout.setprogressbackgroundcolor (r.color.red); //  Set the background of the drop-down circle  mswipelayout.setsize (swiperefreshlayout.large); //Set the size of the circle  

When you set the background and the size of the circle, it looks like this:

More settings can go to the official website Reference document:https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html

To set up listeners:

 Public class extends Implements Swiperefreshlayout.onrefreshlistener {//... }
/** method in listener Swiperefreshlayout.onrefreshlistener, trigger after pull refresh*/     Public voidOnrefresh () {NewHandler (). postdelayed (NewRunnable () {@Override Public voidrun () {//Stop RefreshMswipelayout.setrefreshing (false); }        }, 5000);//send message after 5 seconds, stop refresh}

All code:

/** Created by Storm Zhang, Mar.*/ Packagecom.storm.swiperefreshlayout;Importjava.util.ArrayList;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;Importandroid.support.v4.widget.SwipeRefreshLayout;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ListView;/** * @author: * @description: Stormzhang * @web:http://stormzhang.com/android/2014/10/27/android-swiperefreshlayout/* @date: January 19, 2015*/ Public classMainactivityextendsActivityImplementsSwiperefreshlayout.onrefreshlistener {PrivateSwiperefreshlayout mswipelayout; PrivateListView Mlistview; Privatearraylist<string> list =NewArraylist<string>(); protected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Mlistview=(ListView) Findviewbyid (R.id.listview); Mlistview.setadapter (NewArrayadapter<string> ( This, Android.        R.layout.simple_list_item_1, GetData ())); Mswipelayout=(swiperefreshlayout) Findviewbyid (R.id.swipe_container); Mswipelayout.setonrefreshlistener ( This); //set the color on the drop-down circle, blue, green, orange, redMswipelayout.setcolorschemeresources (Android. R.color.holo_blue_bright, Android. R.color.holo_green_light, Android. R.color.holo_orange_light, Android.        R.color.holo_red_light); Mswipelayout.setdistancetotriggersync (400);//setting how much distance your finger pulls down on the screen triggers a drop-down refreshMswipelayout.setprogressbackgroundcolor (r.color.red);    Mswipelayout.setsize (Swiperefreshlayout.large); }    PrivateArraylist<string>GetData () {List.add ("Hello"); List.add ("This is Stormzhang"); List.add ("An Android Developer"); List.add ("Love Open Source"); List.add ("My Github:stormzhang"); List.add ("Weibo:googdev"); returnlist; }    /** method in listener Swiperefreshlayout.onrefreshlistener, trigger after pull refresh*/     Public voidOnrefresh () {NewHandler (). postdelayed (NewRunnable () {@Override Public voidrun () {//Stop RefreshMswipelayout.setrefreshing (false); }        }, 5000);//send message after 5 seconds, stop refresh    }}

SOURCE Download:http://download.csdn.net/detail/shark0017/8375685

Reference items: Https://github.com/stormzhang/SwipeRefreshLayoutDemo

Reference post: http://stormzhang.com/android/2014/10/27/android-swiperefreshlayout/

Code implementation Android5.0 drop-down refresh effect

Related Article

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.