Android (1) teach you how to use the drop-down refresh

Source: Internet
Author: User

Very long time no open source project, another review of the drop-down refresh is also want to summarize the next project. It's supposed to need those things. Lay the groundwork for yourself, you can also go to the library yourself, about the source code I will put in the back



Pulltorefresh (drop-down refresh) There is a class that is pulltorefreshlistview because I just talk down the refresh, so just take a part of the class.

First copy a package

Paste into your project


Note that this class also requires some resource files:

Layout:header.xml the layout of the head appears pull_to_refresh_header.xml the layout that appears when the refresh assumes your boss doesn't like being able to customize

DRAWABLE-HDPI: There are two pictures under the folder Pulltorefresh_down_arrow.png pulltorefresh_up_arrow.png look at the name to know that a picture is down. A drop-down to a distance appears

You need a res/values/attrs.xml. Define the label yourself


and a res/values/strings.xml.

Add here you can edit the text that appears when the drop-down

    <string name= "Pull_to_refresh_pull_label" >pull to refresh...</string> <string name=    "pull_to_ Refresh_release_label ">release to refresh...</string>    <string name=" Pull_to_refresh_refreshing_ Label ">Loading...</string>    <string name=" Pull_to_refresh_tap_label ">tap to refresh...</ String>

In fact for this open source project. You can understand it as a linear layout, because it has built in a ListView. Just like the listfagment.


I believe everyone has already built the project. I've already said it on the top. Can be used as a linear layout, so you just hold it. Write-first layout with linear layout


, <pre name= "code" class= "HTML" ><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" >    <pulltorefresh. Pulltorefreshlistview        android:id= "@+id/ptrlv"        android:layout_width= "fill_parent"        android:layout_ height= "Fill_parent" >    </pulltorefresh. Pulltorefreshlistview></relativelayout>

<pre name= "code" class= "HTML" >pulltorefresh. Pulltorefreshlistview  is the full name of the Pulltorefreshlistview class to avoid errors, directly copy the package name to open the Pulltorefreshlistview child node right-click to select  Copy qualifiled name<span style= "FONT-SIZE:18PX;" > Start writing Java code </span> since it is defined in XML, it must be instantiated in Java code, <pre name= "code" class= "Java" >    private Pulltorefreshlistview Pulltorefreshlistview;pulltorefreshlistview = (pulltorefreshlistview) findViewById ( R.ID.PTRLV);

Then through Getrefreshableview (); Get a ListView and populate the data with adapter
And then the drop-down refresh has a method setonrefreshlistener drop-down monitoring typically: Request network new data, parse, show

All code

Package Com.example.day24;import Pulltorefresh. Pulltorefreshbase.onrefreshlistener;import Pulltorefresh. Pulltorefreshlistview;import Android.app.activity;import Android.app.actionbar;import Android.app.Fragment;import Android.os.asynctask;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.Menu;import Android.view.menuitem;import Android.view.view;import Android.view.viewgroup;import Android.widget.ArrayAdapter; Import Android.widget.listview;import Android.os.build;public class Mainactivity extends Activity {private Pulltorefre    Shlistview Pulltorefreshlistview;    The/** will be instantiated into the ListView */private ListView LV that is obtained in its own defined linearlayout (Pulltorefreshlistview);        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Pulltorefreshlistview = (Pulltorefreshlistview) Findviewbyid (R.ID.PTRLV); Get a ListView lv= pulltorefreshlistview.g from LinearLayoutEtrefreshableview (); Fills the listview with Data Arrayadapter<string>adapter=new arrayadapter<string> (mainactivity.this,android.        R.layout.simple_list_item_1, InitData ());        Lv.setadapter (adapter); Pulltorefreshlistview.setonrefreshlistener (New Onrefreshlistener () {@Overridepublic void Onrefresh () {//refresh operation.    Typically: Request network new data, parse, show//fake: Async. If you want to Doinbackground (), you can refer to new Myasynct () in Execute (). Execute ();}); }//listview false Data Private string[] InitData () {String [] datas=new string[10];for (int i = 0; i < datas.length; i++) {dat As[i]= "Item" +I;} return datas;} /** generic 1:doinbackground () Parameters::: String URL * Generic 2:onprogressupdate () parameter::: Integer progress * Generic 3:doinbackground () return value type, * Same time also OnPostExecute () the type of the parameter is usually: String byte[] Bitmap*/class myasynct extends Asynctask<string, Integer, string>{@ overrideprotected string Doinbackground (String ... params) {try {thread.sleep ()} catch (Interruptedexception e) { E.printstacktrace ();} return null;} @Overrideprotected void OnposteXecute (String result) {//Async task finished working. 

Tell Pulltorefreshlistview Task finished pulltorefreshlistview.onrefreshcomplete (); Super.onpostexecute (result);}}}

The next chapter will pull down the refresh pull up loading many others together write
Click to download source code

Android (1) teach you how to use the drop-down refresh

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.