Android uses Pulltorefresh to implement a pull load and drop down refresh effect code _android

Source: Internet
Author: User
Tags static class

In not to introduce the text before you, first to introduce the show under the Operation diagram, if you feel that the good, please continue to read:

related reading: sharing the experience of using Pulltorefresh in Android

Project synchronized to: Https://github.com/nanchen2251/pullToRefreshDemo

Simple Use Details:

1 Studio can directly in the app's module settings directly search, but there must be added to, and can not be replaced by space, in order to better understand this thing, I still recommend you to look here, on the Web site:
Https://github.com/chrisbanes/Android-PullToRefresh

So go git up and down. Import the library through studio's Import module function, modify the library's Gradle file to match your own project

Apply plugin: ' Com.android.library '
android {
compilesdkversion
buildtoolsversion "24.0.0"
defaultconfig {
minsdkversion
targetsdkversion
}
buildtypes {release
{
minifyenabled false
proguardfiles getdefaultproguardfile (' proguard-android.txt '), ' Proguard-rules.txt '
}
}
}

2) then add dependencies and associate the app with it.

3 below open the library can see a lot of things, this not only can support ListView, but also can support the GridView and Scollview and so on, is quite comprehensive, but whether and the current fiery recyclerview together with the landlord has not tried.

4 below in our XML layout, here I used the properties of the custom control, so I added a xmlns:app=http://schemas.android.com/apk/res-auto

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
  "http://" Schemas.android.com/apk/res/android "
  xmlns:tools=" Http://schemas.android.com/tools "
  xmlns:app=" http:// Schemas.android.com/apk/res-auto "
  android:layout_width=" match_parent "
  android:layout_height=" Match_ Parent "
  tools:context=" com.example.nanchen.pulltorefreshdemo.MainActivity >
  < Com.handmark.pulltorefresh.library.PullToRefreshListView
    android:layout_width= "Match_parent"
    android: layout_height= "Match_parent"
    android:id= "@+id/main_pull_refresh_lv"
    app:ptranimationstyle= "Flip"
    app:ptrheaderbackground= "@android: color/transparent"
    app:ptrheadertextcolor= "#919191"/>
</RelativeLayout>

5 because we are using its ListView here, we need a Java Bean and a layout of item to customize the layout.

Package Com.example.nanchen.pulltorefreshdemo;
/**
 * Created by South Dust on 16-7-20.
 * * Public
class Music {
  private String title;
  Private String singer;
  Public music () {
  } public
  Music (string title, string singer) {
    this.title = title;
    This.singer = singer;
  }
  Public String GetTitle () {return
    title;
  }
  public void Settitle (String title) {
    this.title = title;
  }
  Public String Getsinger () {return
    singer;
  }
  public void Setsinger (String singer) {
    this.singer = singer;
  }
}

and List_item.xml.

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"http://schemas.android.com/apk" /res/android "
android:layout_width=" match_parent "
android:layout_height=" Match_parent "
android: layout_marginleft= "20DP"
android:layout_marginright= "20DP" >
<textview
android:layout_width= " Wrap_content "
android:layout_height=" wrap_content "
android:id=" @+id/item_title "
android:text=" Song 1 "
android:textsize=" 20sp "
android:layout_alignparentleft=" true "/>
<textview
android: Layout_width= "Wrap_content"
android:layout_height= "wrap_content"
android:id= "@+id/item_singer
" Android:textsize= "20SP"
android:text= "singer 1"
android:layout_alignparentright= "true"/>
</ Relativelayout>

6 The use of very simple, in the activity, the code comments have been more comprehensive, here the simulation of the asynchronous Download Data task, and rewrite the Adaper, as to why the static internal class, this benefits a lot, we can Baidu popular science.

Package Com.example.nanchen.pulltorefreshdemo;
Import Android.content.Context;
Import Android.os.AsyncTask;
Import Android.os.Bundle;
Import android.support.v7.app.AppCompatActivity;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.ListView;
Import Android.widget.TextView;
Import Com.handmark.pulltorefresh.library.ILoadingLayout;
Import Com.handmark.pulltorefresh.library.PullToRefreshBase;
Import Com.handmark.pulltorefresh.library.PullToRefreshListView;
Import java.util.ArrayList;
Import java.util.List;
  public class Mainactivity extends Appcompatactivity {private Pulltorefreshlistview refresh_lv;
  Private list<music> List;
  Private DataAdapter adapter;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_main); REFRESH_LV = (Pulltorefreshlistview) Findviewbyid (r.id.main_pull_refresH_LV);
    List = new arraylist<> ();
    Set up to pull refresh and Drop-down refresh Refresh_lv.setmode (PullToRefreshBase.Mode.BOTH);
    Sets the text displayed when refreshing iloadinglayout startlayout = Refresh_lv.getloadinglayoutproxy (True,false);
    Startlayout.setpulllabel ("Drop-down refresh ...");
    Startlayout.setrefreshinglabel ("Being in a desperate load ...");
    Startlayout.setreleaselabel ("Release to Refresh");
    Iloadinglayout endlayout = Refresh_lv.getloadinglayoutproxy (false,true);
    Endlayout.setpulllabel ("Pull refresh ...");
    Endlayout.setrefreshinglabel ("Being in a desperate load ...");
    Endlayout.setreleaselabel ("Release to Refresh"); Refresh_lv.setonrefreshlistener (New pulltorefreshbase.onrefreshlistener2<listview> () {@Override public void Onpulldowntorefresh (pulltorefreshbase<listview> refreshview) {new Loaddataasynctask (MainActivity.this)
      . Execute (); @Override public void Onpulluptorefresh (pulltorefreshbase<listview> refreshview) {new Loaddat
      Aasynctask (Mainactivity.this). Execute ();
   }
    }); LoadData ();
    adapter = new DataAdapter (this,list);
  Refresh_lv.setadapter (adapter);
  private int count = 1;
      private void LoadData () {for (int i = 0; i < i++) {List.add (New music ("Song" +count, "singer" +count));
    count++; }/** * Asynchronous Download Task * * private static Class Loaddataasynctask extends asynctask<void,void,string>{priv
    Ate mainactivity mainactivity;
    Public Loaddataasynctask (mainactivity mainactivity) {this.mainactivity = mainactivity;
        @Override protected String doinbackground (Void ... params) {try {thread.sleep (2000);
        Mainactivity.loaddata ();
      return "Seccess";
      catch (Interruptedexception e) {e.printstacktrace ();
    return null;
      /** * The method of Completion * * * @Override protected void OnPostExecute (String s) {Super.onpostexecute (s);
        if (S.equals ("seccess")) {mainActivity.adapter.notifyDataSetChanged (); MainactIvity.refresh_lv.onRefreshComplete ();//Refresh Complete}}}/** * Custom adapter * * Private static class DataAdapter E
    Xtends baseadapter{Private context;
    Private list<music> List;
      Public DataAdapter (context context, list<music> List) {this.context = context;
    This.list = list;
      @Override public int GetCount () {if (list!= null) {return list.size ();
    return 0;
    @Override public Object getitem (int position) {return list.get (position);
    @Override public long getitemid (int position) {return position;
      @Override public View getview (int position, View Convertview, ViewGroup parent) {Viewholder vh;
        if (Convertview = = null) {Convertview = Layoutinflater.from (context). Inflate (R.layout.list_item,parent,false);
        VH = new Viewholder ();
        Vh.tv_title = (TextView) Convertview.findviewbyid (r.id.item_title); Vh.tv_singeR = (TextView) Convertview.findviewbyid (R.id.item_singer);
      Convertview.settag (VH);
      }else{VH = (viewholder) convertview.gettag ();
      Music music = (music) getitem (position);
      Vh.tv_title.setText (Music.gettitle ());
      Vh.tv_singer.setText (Music.getsinger ());
    return convertview;
      Class viewholder{TextView Tv_title;
    TextView Tv_singer; }
  }
}

The above is a small set of Android to introduce the use of Pulltorefresh to achieve pull load and pull down the effect of the code, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.