ListView drop-down refresh + pull-up load (demo already available)

Source: Internet
Author: User

1, Xlistview because added header, will result in the storage of data +1, so the assignment needs position-1. Add: When removing headerview, position does not need-1. 2, make a suggestion: pull load more, preferably in OnCreate () in the execution of Setadapter, and then whether it is empty data, or data, only with the update adapter on the line. One, Xlistview 2, usage

Import a Me.maxwin.view package in a diagram

Two interfaces are available:

Ixlistviewlistener: Trigger drop-down refresh, pull up load more. When this interface is implemented, Onloadmore () is used to pull up the load more, Onrefresh () is used to pull the refresh.

Onxscrolllistener: Same as native onscrolllistener, but fires when header/footer rollback.

Several common methods:

Setpullrefreshenable (Boolean enable): Whether to allow drop-down refreshes

Setpullloadenable (Boolean enable): Whether to allow pull-up to load more

Stoprefresh (): Stop refresh, reset header view

Stoploadmore (): Stop loading more, reset footer view

Stop refresh after request to data stops loading more.

Setrefreshtime (String time): Set when the last refresh

Onloadmore (): The method that is called when loading more. Note This method is not called when you first enter.

Onrefresh (): The method that is called when the drop-down is refreshed.

3, how to express the code

1) Implement Ixlistviewlistener interface->2) to achieve pull-up refresh and pull-down load more data changes->3) Update Header view and footer view and set the update time.

[Java]View Plaincopy
    1. 1. Implement Ixlistviewlistener interface
    2. Mlistview.setxlistviewlistener (this);
    3. Data loading for pull-down refreshes in 2.1 onrefresh
    4. @Override
    5. Public void Onrefresh () {
    6. Request data

Update interface Display

[Java]View Plaincopy
    1. OnLoad ();

}//2.2 onloadmore load more data in pull-up loading

[Java]View Plaincopy
    1. @Override
    2. Public void Onloadmore () {
    3. Request data

Update interface Display

[Java]View Plaincopy
    1. OnLoad ();

}//3, after loading the data, reset the header view and footer view, and set the time for the update.

[Java]View Plaincopy
    1. Private void OnLoad () {
    2. Mlistview.stoprefresh (); Mlistview.stoploadmore ();
    3. Mlistview.setrefreshtime ("just");
    4. }
4. XML Considerations

When the Xlistview is embedded in the LinearLayout, the Xlistview can no longer load more when it fills the full screen. On the error code:

[Java]View Plaincopy
  1. <linearlayout
  2. Android:layout_width="Match_parent"
  3. android:layout_height="Wrap_content"
  4. Android:layout_above="@id/takemain_ll"
  5. android:layout_below="@id/takemain_l"
  6. android:orientation="vertical" >
  7. <cn.bestbang.durable_consumer_goods.utils.myexxlistview
  8. android:id="@+id/home_list"
  9. Android:layout_width="Match_parent"
  10. android:layout_height="Match_parent" >
  11. </cn.bestbang.durable_consumer_goods.utils.MyExXListView>
  12. </LinearLayout>
5, to ScrollView common

In this case, overriding Xlistview will cause frequent errors on the pull-up load: The adapter is not updated and does not know how to resolve it.

Second, Pulltorefresh

Most of the content goes from Yang's blog: http://blog.csdn.net/lmj623565791/article/details/38238749

1, the overall understanding:

First, this control on GitHub: Pull-to-refresh, which is very powerful in the example, can support not only the ListView and the GridView. Before downloading, let's say the project structure. GitHub Downloads The example that is dependent on three items, a basic Library_pulltorefresh (the project itself to be quoted. How to quote? Own project Right-->properties->android, select the path in the is library, a basic Pulltorefreshviewpager, a pulltorefreshlistfragment.

2. Usage

1) Set the Pulltorefreshlistview to support pull-up loading and drop-down refresh->2) initialize the control, set the adapter->3) to set the listener event. Handles pull-up loading more and drop-down refreshes in the listener event, setting the time for the last update.

[Java]View Plaincopy
    1. <pre name="code" class="java" ><pre name="code" class="java" >//1, Supports pull-up and drop-down refreshes

Mpullrefreshlistview.setmode (Mode.both);

2. Set adapter Madapter = new Arrayadapter<string> (this, Android. R.layout.simple_list_item_1, Mlistitems); Mpullrefreshlistview.setadapter (Madapter); 3. Set Listener Mpullrefreshlistview.setonrefreshlistener (new onrefreshlistener2<listview> () {@Override public void Onpulldowntorefresh (pulltorefreshbase<listview> Refreshview) {//Displays the last updated time Refreshview.getloadinglayoutproxy (). Setlastupdatedlabel (label); Here write down the refreshed task New Getdatatask (). Execute (); } @Override public void Onpulluptorefresh (pulltorefreshbase<listview> refreshview) {//Displays the last updated time Refreshview.getloadinglayoutproxy (). Setlastupdatedlabel (label);

[Java]View Plaincopy
    1. Here to write up pull load More tasks

New Getdatatask (). Execute (); } });

3. Attribute Introduction

1) ptr:ptrmode= "Both" supports pull-up loading and drop-down refreshes. Disabled disables drop-down refreshes and pull-up loading. Pullfromend only supports pull-up loading. Manualonly only allow manual triggering. Of course through the code can also be set: Lv.setmode (Mode.both);

2) ptr:tranimationstyle= "Flip" flip: Flip animation; rotate: Rotate animation.

3) ptr:ptrdrawable= "@drawable/ic_launcher" setting icon

4) whether the ListView or GridView is allowed to scroll when the ptrscrollingwhilerefreshingenabled is refreshed. Think it's better to be true. 5) ptrlistviewextrasenabled determines the way Header,footer joins Mpullrefreshlistview,true for Headview, which is scrolling when the head refreshes.

4, custom drop-down indicator text content and other effects:

After initialization completes the Pulltorefreshlistview, a Iloadinglayout object can be obtained by Lv.getloadinglayoutproxy (), which sets the styles, text, etc. in various indicators.

[Java]View Plaincopy
    1. Iloadinglayout startlabels = Mpullrefreshlistview
    2. . Getloadinglayoutproxy ();
    3. Startlabels.setpulllabel ("You can pull, pull ..."); The prompt that appears when you just drop down
    4. Startlabels.setrefreshinglabel ("OK, is refreshing ..."); When refreshed
    5. Startlabels.setreleaselabel ("You dare to put, I dare to refresh ..."); When it reaches a certain distance, the displayed hints

The default is that the pull-up and drop-down words change at the same time, if you want to change them individually:

[Java]View Plaincopy
  1. Private void Initindicator () {
  2. Iloadinglayout startlabels = Mpullrefreshlistview
  3. . Getloadinglayoutproxy (True, false);
  4. Startlabels.setpulllabel ("You can pull, pull ..."); The prompt that appears when you just drop down
  5. Startlabels.setrefreshinglabel ("OK, is refreshing ..."); When refreshed
  6. Startlabels.setreleaselabel ("You dare to put, I dare to refresh ..."); When it reaches a certain distance, the displayed hints
  7. Iloadinglayout endlabels = Mpullrefreshlistview.getloadinglayoutproxy (
  8. false, true);
  9. Endlabels.setpulllabel ("You can pull, pull 2 ..."); The prompt that appears when you just drop down
  10. Endlabels.setrefreshinglabel ("OK, refreshing 2 ..."); When refreshed
  11. Endlabels.setreleaselabel ("You dare to put, I will dare to refresh 2 ..."); When it reaches a certain distance, the displayed hints
  12. }

Copyright Notice:

ListView drop-down refresh + pull-up load (demo already available)

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.