Android Project: Use Pulltorefresh open source project extension to pull up the pull-up load more processing method, monitor ListView scroll Direction

Source: Internet
Author: User

Many Android apps drop-down refreshes are used by the Pulltorefresh open source project, but its extensibility will have some limitations when it comes up with a pull-down refresh while loading more. Looked at a lot of places, found that this open source project is not very good at the same time to support the drop-down refresh and pull-up load more. This component has a mode property that can be set to both, which can be pulled up and down at the same time. But with this property set, the effect of pull-up and pull-down is exactly the same. So to use this open source project for pull-down refreshes and pull up loads more at the same time, you need to do some processing in the code.

==========================pulltorefresh Property-related =====================================

Open Source project: Android-pulltorefresh

Project Address: Https://github.com/chrisbanes/Android-PullToRefresh/wiki/Quick-Start-Guide

1. Properties: Https://github.com/chrisbanes/Android-PullToRefresh/blob/master/library/res/values/attrs.xml

Namespaces: xmlns:ptr= "Http://schemas.android.com/apk/res-auto"

Ptr:ptranimationstyle The animation effect provides two values of flip and rotate default to rotate
Ptr:ptrrefreshableviewbackground Set the background color of the refresh view
Ptr:ptrheaderbackground Set the background color of the head view
Ptr:ptrheadertextcolor Set the color of the head view text
Ptr:ptrheadersubtextcolor Set the color of the header view subtitle text
Ptr:ptrmode

Pullfromstart:

Pullfromend:

Both

Setonrefreshlistener (Onrefreshlistener Listener): Set the refresh listener;

Setonlastitemvisiblelistener (Onlastitemvisiblelistener Listener): Sets whether to the bottom listener;

Setonpulleventlistener (Onpulleventlistener listener); Set event listeners;

Onrefreshcomplete (): Set Refresh Complete

========================== Monitor ListView Scroll Direction =====================================

The key to the

Modify for pull-up loading is to determine the scrolling direction of the ListView before the Onrefresh method executes. The following method is the best one of the methods tried, and cannot be said to be a perfect solution, but it should be the one with the closest effect. (There may still be a bit of error if the first-screen data rows are not filled with the screen, or if scrolling is less than the line height for scrolling.) However, most of the application's list of data per page can be filled with screens, and this method can be improved with other gestures.

int mlastfirstvisibleitem = 0;boolean misscrollingup;  @Override public void onscrollstatechanged ( Abslistview view, int scrollstate) {if (view.getid () = = Mlistview.getid () {final int currentfirstvisibleitem = Mlistview.getfirstvisibleposition (); if (Currentfirstvisibleitem > Mlastfirstvisibleitem) {misup = true;} span class= "keyword" >else if (Currentfirstvisibleitem < Mlastfirstvisibleitem) {Misup = false; } Mlastfirstvisibleitem = Currentfirstvisibleitem; } }

Reference: http://stackoverflow.com/questions/12114963/detecting-the-scrolling-direction-in-the-adapter-up-down/ 12115157#12115157

========================== implement pull-down refresh and pull-up load more ====================================

After parsing the JSON, determine whether it is a pull-up or a drop-down refresh operation:

Parse jsonvoid Parsejson (String result) {    "key");                                                             if (!misup)    {        mdatalist.clear ();    }    Mdatalist.addall (locallist);}  

After the data has been loaded, notifydatasetchanged and notification Pullrefreshlistview, and the page number plus 1:

Finished loading processing  void LoadComplete () {    mpullrefreshlistview.onrefreshcomplete ();    Madapter.notifydatasetchanged ();    1;} 

To determine the pull-up and pull-down direction, listen to refresh the ListView, modify the header and bottom view text description:

/** * ******************* pull-down refresh with pull-up load listening processing ************************ */Refresh the ListView Listener@OverridePublicvoid Onrefresh (pulltorefreshbase<listview> refreshview) {Get refresh time, set refresh time format String str = dateutils.formatdatetime (getactivity (), System.currenttimemillis (), Dateutils.format_ Numeric_date | Dateutils.format_no_noon);Judging the pull-down or the pull-upif (!misup) MPage =0;Set the Refresh text description (during refresh)if (misup) {mpullrefreshlistview.getloadinglayoutproxy (). Setrefreshinglabel ("Loading"); Mpullrefreshlistview.getloadinglayoutproxy (). Setpulllabel ("Pull up load more"); Mpullrefreshlistview.getloadinglayoutproxy (). Setreleaselabel ("Release start loading"); Refreshview.getloadinglayoutproxy (). Setlastupdatedlabel ("Last Load Time:" + str);}else {mpullrefreshlistview.getloadinglayoutproxy (). Setrefreshinglabel ("Refreshing"); Mpullrefreshlistview.getloadinglayoutproxy (). Setpulllabel ("Drop-down refresh"); Mpullrefreshlistview.getloadinglayoutproxy (). Setreleaselabel ("Release start Refresh"); Refreshview.getloadinglayoutproxy (). Setlastupdatedlabel ("Last Update Time:" + str);}Start download task, load data Loadtask ();}int Mlastfirstvisibleitem =0;Boolean Misscrollingup;@OverridePublicvoid Onscrollstatechanged (Abslistview view,int scrollstate) {if (view.getid () = = Mlistview.getid ()) {Finalint currentfirstvisibleitem = Mlistview.getfirstvisibleposition ();if (Currentfirstvisibleitem > Mlastfirstvisibleitem) {misup =True }Elseif (Currentfirstvisibleitem < Mlastfirstvisibleitem) {Misup =False } Mlastfirstvisibleitem = Currentfirstvisibleitem; } }@OverridePublicvoid Onscroll (Abslistview view,int Firstvisibleitem,int VisibleItemCount,int Totalitemcount) {Set the Refresh text description (before you expand the Refresh bar)if (misup) {mpullrefreshlistview.getloadinglayoutproxy (). Setrefreshinglabel ("Loading"); Mpullrefreshlistview.getloadinglayoutproxy (). Setpulllabel ("Pull up load more"); Mpullrefreshlistview.getloadinglayoutproxy (). Setreleaselabel ("Release start loading"); }else {mpullrefreshlistview.getloadinglayoutproxy (). Setrefreshinglabel ("Refreshing"); Mpullrefreshlistview.getloadinglayoutproxy (). Setpulllabel ("Drop-down refresh"); Mpullrefreshlistview.getloadinglayoutproxy (). Setreleaselabel ("Release start Refresh"); } }@OverridePublicvoid Onlastitemvisible () {misup =true;}

/////////////////////////////////////////////////////////////////////////////////////////
Lvvideo.setonscrolllistener (New Onscrolllistener () {

@Override
public void onscrollstatechanged (Abslistview view, int scrollstate) {

Switch (scrollstate) {

Before scrolling, the hand also records the subscript before scrolling on the screen.
Case Onscrolllistener.scroll_state_touch_scroll:
View.getlastvisibleposition () Gets the index of the first item visible on the current screen in the entire ListView
Lvindext=view.getlastvisibleposition ();
Break

Scrolling stop
Case Onscrolllistener.scroll_state_idle:
Record the position of the current item after a scrolling stop
int scrolled=view.getlastvisibleposition ();
Scroll down after the subscript is greater than the scroll before scrolling
if (Scrolled>lvindext) {
Scroll = false;
Uihelper.toastmessage (videomain.this, "menu pick Up");
}
Scroll up.
else{
Uihelper.toastmessage (videomain.this, "menu Popup");
Scroll = true;
}
Break

//////////////////////////////////////////////////////////////////////////////////////////////////////
Onscrolllistener's Onscroll () has a firstvisibleitem parameter (the second parameter), the downward slide will be bigger and larger, the upward slide will be more and more small, you can slide the time to store this value, and then to judge the current value

Alternatively, it can be judged by view.getlastvisibleposition (), which returns the bottom of the screen.
< span class= "keyword" >< span class= "keyword" >< span class= "annotation" >     

Android Project: Use Pulltorefresh open source project extension for pull-down refresh pull-up load more processing method, monitor ListView scroll Direction

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.