android--Google official drop-down refresh control swiperefreshlayout

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/zouzhigang96/article/details/50476402

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Objective:
Now Google has introduced a more official dropdown refresh control, which is undoubtedly good news for Android developers, and it is convenient to use the Swiperefreshlayout control to implement the pull-down refresh feature. Android4.0 The following versions need to use the Android-support-v4.jar package in order to use
Android-support-v4.jar Bag: http://download.csdn.net/detail/h7870181/7784247

Note: Be sure to upgrade the support library version to 19.1 or the latest

Google Swiperefreshlayout official API

So let's do a simple demo and see how swiperefreshlayout this control

Note: The landlord is using Android Studio as the IDE.

1, first look at the layout file of Swiperefreshlayout

<?xml version= "1.0" encoding= "Utf-8"?><Android.support.v4.widget.SwipeRefreshLayoutXmlns:android="Http://schemas.android.com/apk/res/android"android:id= "@+id/swiperereshlayout" android:layout_margintop= "55DP" 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 "></listview></ ANDROID.SUPPORT.V4.WIDGET.SWIPEREFRESHLAYOUT>       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

2, how the swiperefreshlayout is used in activity

PublicClassMainactivityExtendsappcompatactivity {Private Swiperefreshlayout swiperereshlayout;Private ListView ListView;Private arrayadapter<string> adapter;Private list<string> data;@OverrideProtectedvoidOnCreate (Bundle savedinstancestate) {Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Toolbar Toolbar = (Toolbar) Findviewbyid (R.id.toolbar); Setsupportactionbar (toolbar); Floatingactionbutton fab = (Floatingactionbutton) Findviewbyid (R.id.fab); Fab.setonclicklistener (New View.onclicklistener () {@OverridePublicvoidOnClick (view view) {Snackbar.make (view,"Replace with your own action", Snackbar.length_long). Setaction ("Action",NULL). Show (); } }); Initview (); }PrivatevoidInitview () {swiperereshlayout = (swiperefreshlayout) Findviewbyid (r.id.swiperereshlayout); listview = (ListView) Findviewbyid (R.id.listview); data =New Arraylist<string> ();for (int i =0; I <20; i++) {Data.add ("The current item is" + i);} adapter =New Arrayadapter<string> (mainactivity.This, Android. R.layout.simple_list_item_1, data); Listview.setadapter (adapter); Swiperereshlayout.setcolorschemeresources (Android. R.color.holo_blue_bright, Android. R.color.holo_green_light, Android. R.color.holo_orange_light);//to Swiperefreshlayout binding Refresh listener Swiperereshlayout.setonrefreshlistener ( new Swiperefreshlayout.onrefreshlistener () { @Override Span class= "Hljs-keyword" >public void onrefresh () {//sets the time for 2 seconds to perform the following event new Handler (). postdelayed ( New Runnable () {public void run () { Data.add (0,  "new item after Refresh"); adapter.notifydatasetchanged (); Swiperereshlayout.setrefreshing (false);}, 2000); } }); }}
      1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • Ten
    • one
    • 2
    • (
    • )
    • +
    • +
    • /
    • 0
    • +
    • all
    • +
    • +
    • +
    • -
    • 29
    • +
    • +
    • all
    • +
    • +
    • PNS
    • up
    • i>39
    • 48
    • all
    • /
    • /
    • /
    • /li>
    • ,
    • ,
    • ,
    • up-
    • -
    • +
    • -
    • +
    • +
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

Note: The main code is in the Initview this method inside, the simple implementation of how to pull the new data, just do a simple demonstration, specific in-depth development, we can also try to customize.

Main methods

(1) Setonrefreshlistener (Onrefreshlistener): Add a listener to the layout

(2) Setrefreshing (Boolean): Show or hide the Refresh progress bar

(3) isrefreshing (): Check whether it is in a refreshed state

(4) Setcolorschemeresources (): Set the color theme of the progress bar, can set up to four kinds of

Currently deprecated with Swiperereshlayout.setcolorscheme ()
You can use Swiperereshlayout.setcolorschemeresources () to set the color.

Summarize:

Google is now constantly refining its own SDK, the introduction of more and more components, the purpose is to make development simpler, more unified design, this may be the future direction of Google, in any case, this is undoubtedly very good news for developers.

SOURCE download

android--Google official dropdown refresh control swiperefreshlayout (GO)

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.