Android project: Use the pulltorefresh open-source project extension to pull more processing methods from the pull-down refresh and listen for listview scrolling.

Source: Internet
Author: User

Many android applications use the pulltorefresh open-source project for pull-down refresh. However, its scalability has some limitations when pulling and loading more resources. I checked a lot and found that this open-source project does not support pull-down refresh and pull-up loading at the same time. This component has a mode attribute, which can be set to both, that is, both upper and lower can be pulled. However, if you only set this attribute, the pull-up and pull-down operations will have the same effect. Therefore, to use this open-source project to pull down and refresh and load more resources, you need to perform some processing in the code.



====================================== Pulltorefresh ======================================

Open-source projects: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

Namespace: xmlns: ptr = "http://schemas.android.com/apk/res-auto"

Ptr: ptrAnimationStyle The animation effect provides two values: flip and rotate. The default value is rotate.
Ptr: ptrRefreshableViewBackground Set the background color of the refreshed View.
Ptr: ptrHeaderBackground Set the background color of the header View.
Ptr: ptrHeaderTextColor Set the color of the View text in the header
Ptr: ptrHeaderSubTextColor Set the color of the header view subtitle text
Ptr: ptrMode

PullFromStart:

PullFromEnd:

Both;


2. Common Methods

SetOnRefreshListener (OnRefreshListener listener): sets the refresh listener;

SetOnLastItemVisibleListener (OnLastItemVisibleListener listener): sets whether the listener goes to the bottom;

SetOnPullEventListener (OnPullEventListener); sets the event listener;

OnRefreshComplete (): Set to refresh complete


=================================================================== ======================================


The key to changing to pull-up loading is to determine the scrolling direction of the listview before the onrefresh method is executed. The following method is the best one in the tried method. It cannot be a perfect solution, but it should be the most effective one. (When the number of rows on the first screen is not full, or the scroll distance of the first line is smaller than the Row Height, there may still be some error. However, the list of most applications can be filled with screens on every page, and the method can be improved based on other gestures ).


int mLastFirstVisibleItem = 0;boolean mIsScrollingUp;@Overridepublic void onScrollStateChanged(AbsListView view, int scrollState){                                                                                                         if (view.getId() == mListView.getId())    {        final int currentFirstVisibleItem = mListView.getFirstVisiblePosition();                                                                                                                 if (currentFirstVisibleItem > mLastFirstVisibleItem)        {            mIsUp = true;        }        else if (currentFirstVisibleItem < mLastFirstVisibleItem)        {            mIsUp = false;        }        mLastFirstVisibleItem = currentFirstVisibleItem;    }                                                                                                     }


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




====================================== Implement pull-down refresh and pull-up loading more ====== ====================================

After the json Parsing is completed, determine whether to pull up or pull down and refresh:

// Parse jsonprivate void parseJson (String result) {List <ListJson> localList = parseJsonArray (Utils. parseListJson (result, "key"); if (! MIsUp) {mDataList. clear ();} mDataList. addAll (localList );}


After the data is loaded, yydatasetchanged and the PullRefreshListView are notified, and the page number is added to 1:

// Private void loadComplete () {mPullRefreshListView. onRefreshComplete (); mAdapter. policydatasetchanged (); mPage + = 1 ;}


Determine the pull-up and drop-down directions, listen to refresh the listview, and modify the text description of the header and the bottom view:

/********************* Process pull-down refresh and pull-up listening ******** **************** // refresh the listview listener @ Overridepublic void onRefresh (PullToRefreshBase <ListView> refreshView) {// obtain the refresh time. Set the refresh time format to String str = DateUtils. formatDateTime (getActivity (), System. currentTimeMillis (), DateUtils. FORMAT_NUMERIC_DATE | DateUtils. FORMAT_NO_NOON); // determines whether to pull the drop-down list or if (! MIsUp) mPage = 0; // set the refresh text description (during refreshing) if (mIsUp) {mPullRefreshListView. getLoadingLayoutProxy (). setRefreshingLabel ("loading"); mPullRefreshListView. getLoadingLayoutProxy (). setPullLabel ("pull up to load more"); mPullRefreshListView. getLoadingLayoutProxy (). setReleaseLabel ("Release starts loading"); refreshView. getLoadingLayoutProxy (). setLastUpdatedLabel ("last loading time:" + str);} else {mPullRefreshListView. getLoadingLayoutProxy (). setRefreshingLabel ("refreshing"); mPullRefreshListView. getLoadingLayoutProxy (). setPullLabel ("pull-down refresh"); mPullRefreshListView. getLoadingLayoutProxy (). setReleaseLabel ("Release starts refreshing"); refreshView. getLoadingLayoutProxy (). setLastUpdatedLabel ("Last Update time:" + str) ;}// start the download task and load the data loadTask () ;}int mLastFirstVisibleItem = 0; boolean mIsScrollingUp; @ Overridepublic void onScrollStateChanged (AbsListView view, int scrollState) {if (view. getId () = mListView. getId () {final int currentFirstVisibleItem = mListView. getFirstVisiblePosition (); if (currentFirstVisibleItem> mLastFirstVisibleItem) {mIsUp = true;} else if (currentFirstVisibleItem <strong) {mIsUp = false;} Then = currentFirstVisibleItem ;}} @ Overridepublic void onScroll (AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {// set the text description for refreshing (before expanding the refresh column) if (mIsUp) {mPullRefreshListView. getLoadingLayoutProxy (). setRefreshingLabel ("loading"); mPullRefreshListView. getLoadingLayoutProxy (). setPullLabel ("pull up to load more"); mPullRefreshListView. getLoadingLayoutProxy (). setReleaseLabel ("Release starts loading");} else {mPullRefreshListView. getLoadingLayoutProxy (). setRefreshingLabel ("refreshing"); mPullRefreshListView. getLoadingLayoutProxy (). setPullLabel ("pull-down refresh"); mPullRefreshListView. getLoadingLayoutProxy (). setReleaseLabel ("Release start refresh") ;}@ Overridepublic void onLastItemVisible () {mIsUp = true ;}











This article is from the "no trace in the sky but I fly over" blog. For more information, contact the author!

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.