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

Source: Internet
Author: User

Long time no open source project, re-review the drop-down refresh is also want to summarize the next project, should need those things, lay the foundation for themselves, you can also go to the library, about the source I will be in the back



Pulltorefresh (drop-down refresh) There is a class that is pulltorefreshlistview because I only speak the drop-down refresh, so only a subset of the classes are taken. First copy a package

Paste into your project


It is important to 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 you refresh if your boss doesn't like it, you can customize it.

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

You also need a res/values/attrs.xml custom label


and a res/values/strings.xml.

Join 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 as a linear layout, because it built a ListView, just like Listfagment.


I believe that we have already set up the project, I have said above, can be used as a linear layout, so you have to take when the linear layout with the first write 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;    /** will be instantiated as a custom LinearLayout (Pulltorefreshlistview) to get the ListView */private ListView LV;        @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.ge from LinearLayoutTrefreshableview (); 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 pass the parameter 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 () parameter::: String URL * Generic 2:onprogressupdate () parameter::: Integer progress * Generic 3:doinbackground () return value type, * also OnPostExecute () parameter type usually: String byte[] Bitmap*/class myasynct extends Asynctask<string, Integer, string>{@Ov errideprotected string Doinbackground (String ... params) {try {thread.sleep ()} catch (Interruptedexception e) { E.printstacktrace ();} return null;} @Overrideprotected void OnpostexecUte (String result) {//Async task completes work. Tell Pulltorefreshlistview task to complete pulltorefreshlistview.onrefreshcomplete (); Super.onpostexecute (result);}}}
The next chapter will pull down the refresh pull-up load more together write on
Click to download the 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.