Android Official dropdown Refresh Control swiperefreshlayout use detailed _android

Source: Internet
Author: User
Tags addall

Most of the people who might have developed Android have used many open source components that have been pulled down, but today the official V4 support Package Swiperefreshlayout feel pretty good, especially to share.

Brief introduction:
The Swiperefreshlayout component accepts only one subassembly: that is, the component that needs to be refreshed. It uses a listening mechanism to notify listeners owning the component that a refresh event is occurring, in other words, that our activity must implement the interface of the notification. The activity is responsible for handling event refreshes and refreshing the corresponding views. Once the listener receives the event, it determines where it should be handled during the refresh process. If you want to show a refresh animation, it must call Setrefrshing (True) or cancel the animation to call Setrefreshing (false).

And this thing only supports Drop-down refreshes, you can customize view to inherit it to implement your pull load.

How to use Swiperefreshlayout:

It's very simple to use.

Here's an example of how to use the official this drop-down to refresh the control, at the end I also imitate the virtual implementation of a small pull load effect, can be for everyone to analyze upward.

First figure:

Up Point code:

Activity_main.xml

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
 "http://" Schemas.android.com/apk/res/android "
 xmlns:tools=" Http://schemas.android.com/tools "
 android:layout_ Width= "Match_parent"
 android:layout_height= "match_parent"
 tools:context= " Com.example.nanchen.swiperefreshlayoutdemo.MainActivity ">
 < Android.support.v4.widget.SwipeRefreshLayout
  android:id= "@+id/main_srl"
  android:layout_width= "Wrap_ Content "
  android:layout_height=" wrap_content ">
  <listview
   android:id=" @+id/main_lv
   " Android:layout_width= "Wrap_content"
   android:layout_height= "wrap_content" >
  </ListView>
 </android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>

Menu_layout.xml

<?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 ">
 < ProgressBar
  style= "? Android:attr/progressbarstylesmall"
  android:layout_width= "Wrap_content"
  android:layout_height= "Wrap_content"
  android:id= "@+id/progressbar"/>
 <textview
  android: Layout_width= "Wrap_content"
  android:layout_height= "wrap_content"
  android:text= "is dying to load ..."
  Android:id= "@+id/textview2"/>
</LinearLayout>

Mainactivity.java

Package Com.example.nanchen.swiperefreshlayoutdemo;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.support.v4.widget.SwipeRefreshLayout;
Import android.support.v7.app.AppCompatActivity;
Import Android.view.View;
Import Android.widget.AbsListView;
Import Android.widget.ArrayAdapter;
Import Android.widget.ListView;
Import java.util.ArrayList;
Import Java.util.Arrays;
 
Import java.util.List; public class Mainactivity extends Appcompatactivity implements Abslistview.onscrolllistener {private
 Swiperefreshlayout swiperefreshlayout;
 Private ListView LV;
 Private Arrayadapter adapter;
 Private list<string> List;
 Private View Footerview; Private Handler Handler = new Handler () {@Override public void handlemessage (message msg) {Super.handlemessage (msg
   );
      Switch (msg.what) {case 0x101:if (swiperefreshlayout.isrefreshing ()) {adapter.notifydatasetchanged ();
  Swiperefreshlayout.setrefreshing (false);/Set not refreshed   } break;

 }
  }
 };
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Setcontentview (R.layout.activity_main);
  Swiperefreshlayout = (swiperefreshlayout) Findviewbyid (R.ID.MAIN_SRL);
  LV = (ListView) Findviewbyid (R.ID.MAIN_LV);
  Footerview = Getlayoutinflater (). Inflate (R.layout.loading_layout,null);
  Lv.addfooterview (Footerview);
  Lv.setonscrolllistener (this);
  List = new arraylist<> (); List.addall (Arrays.aslist ("Java", "PHP", "C + +", "C #", "IOS", "html", "C", "Java ee", "j2se", "VB", ". Net", "Http", "TCP", "UDP
  "," www ")); adapter = new Arrayadapter (this,android. R.layout.simple_list_item_1, Android.

  R.id.text1,list);
  Lv.setadapter (adapter); Swiperefreshlayout.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); Swiperefreshlayout.setonrefreshlistener (New Swiperefreshlayout.onrefreshlistener () {@Override
   public void Onrefresh () {new Loaddatathread (). Start ();
 }
  }); The index of the last data that private int visiblelastindex;//uses to display @Override public void onscrollstatechanged (Abslistview view, int Scrol Lstate) {if (Adapter.getcount () = Visiblelastindex && scrollstate = = scroll_state_idle) {New Loaddatathread (
  ). Start (); @Override public void Onscroll (Abslistview view, int firstvisibleitem, int visibleitemcount, int Totalitemcoun T) {Visiblelastindex = Firstvisibleitem + visibleitemcount-1;//minus the last load of that}/** * Simulate loading Data thread/class Loaddat
   Athread extends thread{@Override public void Run () {initdata ();
   try {thread.sleep (2000);
   catch (Interruptedexception e) {e.printstacktrace (); } handler.sendemptymessage (0x101);//Send an updated data by handler token} private void InitData () {List.addall ("J

  Son "," XML "," UDP "," http "); 


 }
 }
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.