[Go] Android pulltorefresh extension Recyclerview

Source: Internet
Author: User

Reprint please indicate the source:
http://blog.csdn.net/xuehuayous/article/details/50387089
This article is from: "Kevin.zhou's Blog"

Preface: Next, "Android Pulltorefresh analysis, Response gesture event", this article mainly analyzes how to expand Pulltorefreshbase, to create a variety of refreshing loading content area.

First, review

In the second chapter, "Pulltorefresh analysis of the second, UI structure," put forward four questions, just a simple rough said how to solve, did not go to see the source code, the following first put these four questions to take out:
    1. What is the direction of the flush loading, usually vertical, in case the demand of the wonderful flower is raised horizontally?
    2. What should the content area show? How to set up to expand?
    3. How to judge to the top, to trigger the refresh action?
    4. How to determine the bottom, to trigger the load operation?

Second, source code analysis

or simple ScrollView as an example of the analysis, for Mao we are always bullying PulltorefreshScrollView, not anxious analysis ScrollView we analyze Pulltorefreshlistview. Cui Hua, on the code!

(a), Pulltorefreshscrollview analysis First look at what are the methods:

In addition to the construction method, there are only four methods, namely Getpulltorefreshscrolldirection (), Createrefreshableview (), Isreadyforpullstart (), Isreadyforpullend (). These four methods can be seen by the name of the corresponding four questions we raised, then hit analysis:

1. What is the refresh direction? For this question you must know that is vertical, the code is also relatively simple:
@Overridepublic final Orientation getpulltorefreshscrolldirection () {return orientation.vertical;}


2. What does the "content area" show?

Because it's pulltorefreshscrollview, it's definitely generating a ScrollView object:
@Overrideprotected ScrollView Createrefreshableview (context context, AttributeSet attrs) {ScrollView scrollview;if ( VERSION. Sdk_int >= version_codes. Gingerbread) {ScrollView = new InternalScrollViewSDK9 (context, attrs);} else {scrollView = new ScrollView (context, attrs) ;} Scrollview.setid (R.id.scrollview); return scrollView;}

You can see that if the current system version is greater than 9 created is InternalScrollVIewSDK9, the other case is the creation of the system's ScrollView, then this internalscrollview what is it, to see:

 @TargetApi (9) Final class InternalScrollViewSDK9 extends ScrollView {public InternalScrollViewSDK9 (context context, AttributeSet Attrs) {Super (context, attrs);} @Overrideprotected boolean Overscrollby (int deltax, int deltay, int scrollx, int scrolly, int scrollrangex, int Scrollrang  EY, int maxoverscrollx, int maxoverscrolly, Boolean istouchevent) {Final Boolean returnvalue = Super.overscrollby (DeltaX, DeltaY, Scrollx, scrolly, Scrollrangex,scrollrangey, Maxoverscrollx, maxoverscrolly, istouchevent);//Does all of the Har D work ... Overscrollhelper.overscrollby (Pulltorefreshscrollview.this, DeltaX, Scrollx, DeltaY, ScrollY,getScrollRange (), istouchevent); return returnvalue;} /*** taken from the AOSP ScrollView source*/private int getscrollrange () {int scrollrange = 0;if (Getchildcount () > 0) {View child = getchildat (0); scrollrange = Math.max (0, Child.getheight ()-(GetHeight ()-Getpaddingbottom ()-getpaddingto P ()));} return scrollrange;}} 
You can see the scrollview of the class or the inherited system, but only two methods Overscrollby () and Getscrillrange () are covered. So why do you want to overwrite these two methods? Overscrollby () is a method provided by android2.3 to enable over-scrolling of the view, which can continue scrolling after scrolling to the head. The default is no such effect, and here does not have to relate to it.

3. How to determine the top, to trigger the refresh action?

Judgment reached the top, this time can trigger a series of refresh loading operation, because the ScrollView can only contain a sub-view, so long as the child view of the head is completely displayed.
@Overrideprotected Boolean Isreadyforpullstart () {return mrefreshableview.getscrolly () = = 0;}
just a simple line of code, what does this getscrolly () mean? Google's comments to us areReturn the scrolled top position of this view. The approximate meaning is to get the ScrollView within the sub-view has been scrolling upward, then if the distance is 0, the child view of the head is fully displayed.4. How to determine the bottom, to trigger the load operation? Judgment reached the bottom of the effect is actually the same as the judgment to reach the top, in the scrollview we just have to judge the sub-view has been scrolled to the bottom can:
@Overrideprotected Boolean isreadyforpullend () {View scrollviewchild = mrefreshableview.getchildat (0); if (null! = Scrollviewchild) {return mrefreshableview.getscrolly () >= (Scrollviewchild.getheight ()-getheight ());} return false;}
As you can see from the code, the distance that has been scrolled up + the height of the ScrollView = the height of the child view can be judged to have reached the bottom.(ii), Pulltorefreshlistview analysisFirst look at the inheritance of the following classes:


And Pulltorefreshscrollview directly inherit pulltorefreshbase different is more a middle pulltorefreshadapterviewbase, Then according to generational Pulltorefreshscrollview should be pulltorefreshlistview uncle or uncles. Well, then, the four ways to find our relationship:


Unfortunately, we just found two getpulltorefershscrilldirection () and Createrefreshableview (), so look at these two methods first:1. What is the refresh direction?
@Overridepublic final Orientation getpulltorefreshscrolldirection () {return orientation.vertical;}
This is also a return to the vertical. 2. What does the "content area" show?
@Overrideprotected ListView Createrefreshableview (Context context, AttributeSet Attrs) {ListView LV = Createlistview ( context, attrs);//Set it to the so it can be used in Listactivity/listfragmentlv.setid (Android. r.id.list); return LV;}
Here, as we expected, a ListView object is returned.3. How to determine the top, to trigger the refresh action? We are looking for isreadyforpullstart (), but in the pulltorefreshlistview we do not find its trace, then go to its parent class to see, sure enough inPulltorefreshadapterviewbase found its figure:
@Overrideprotected Boolean Isreadyforpullstart () {return isfirstitemvisible ();}

A isfirstitemvisible () method is called, and it is also better understood that determining whether a refresh action can be triggered is based on judging whether the first item of the ListView is fully visible:

Private Boolean isfirstitemvisible () {final Adapter Adapter = Mrefreshableview.getadapter (), ... if ( Mrefreshableview.getfirstvisibleposition () <= 1) {final View firstvisiblechild = mrefreshableview.getchildat (0); if (Firstvisiblechild! = null) {return firstvisiblechild.gettop () >= mrefreshableview.gettop ();} return false;}

As you can see, this gets the first entry in the ListView and then determines whether the entry is fully visible.


4. How to determine the bottom, to trigger the load operation? Here andIsreadyforpullstart () similar toIsreadyforpullend () is also inin Pulltorefreshadapterviewbase:
Protected Boolean isreadyforpullend () {return islastitemvisible ();}

In the same way, it also calls a islastitemvisible () method:

Private Boolean islastitemvisible () {final Adapter Adapter = Mrefreshableview.getadapter (); final int Lastitemposition = Mrefreshableview.getcount ()-1;   Final int lastvisibleposition = Mrefreshableview.getlastvisibleposition (); if (Lastvisibleposition >= lastItemPosition-1) {final int childindex = Lastvisibleposition-mrefreshableview.getfirstvisibleposition (); final View lastvisiblechild = Mrefreshableview.getchildat (Childindex), if (lastvisiblechild! = null) {return Lastvisiblechild.getbottom () <= mrefreshableview.getbottom ();}} return false;}

This is determined by judging whether the last item visible is the last one and whether it is fully visible to reach the bottom.

iii. expansion of Recyclerview Through the above analysis, we can know that if you want to expand the words only need to inherit pulltorefreshbase and according to the extended view properties to implement these four methods can be, is not relatively simple. 1. Inheritance pulltorefreshbase:



2. We find that there are errors, and then implement the abstract method:

3. There are also errors, which are missing constructors:

public class Pulltorefreshrecyclerview extends Pulltorefreshbase<recyclerview>{public Pulltorefreshrecyclerview (Context context) {super (context);} Public Pulltorefreshrecyclerview (context context, AttributeSet Attrs) {Super (context, attrs);} Public Pulltorefreshrecyclerview (Context context, Mode mode) {Super (context, mode);} Public Pulltorefreshrecyclerview (Context context, mode mode, Animationstyle Animstyle) {Super (context, mode, Animstyle) ;} @Overridepublic Orientation getpulltorefreshscrolldirection () {return null;} @Overrideprotected Recyclerview Createrefreshableview (context context, AttributeSet attrs) {return null;} @Overrideprotected Boolean isreadyforpullend () {return false;} @Overrideprotected Boolean Isreadyforpullstart () {return false;}}

OK, so far, inherit a class, fix the error, 5 seconds to finish the shelf, and then fill in the meat. 4. Set the refresh load direction
@Overridepublic Orientation getpulltorefreshscrolldirection () {return orientation.vertical;}

Since the effect we want is a vertical flush load, this is set to VERTICAL.

5. "Content Area" object creation

@Overrideprotected Recyclerview Createrefreshableview (context context, AttributeSet attrs) {Recyclerview Recyclerview = new Recyclerview (context, attrs);    return Recyclerview;}
It's easier here, new one recyclerview. 6. How to judge the top
@Overrideprotected Boolean Isreadyforpullstart () {return isfirstitemvisible ();}

Here we imitate the settings of the next Pulltorefreshlistview, engage a isfirstitemvisible (), then the focus is the method of writing:

Private Boolean isfirstitemvisible () {final adapter<?> Adapter = Getrefreshableview (). Getadapter ();// If adapter is not set or adapter No data can be pulled down to refresh if (Null = = Adapter | | Adapter.getitemcount () = = 0) {if (DEBUG) {log.d (Log_tag, "isfirstit Emvisible. Empty View. ");} return true;} else {//The first entry is fully displayed and can be refreshed if (getfirstvisibleposition () = = 0) {return mrefreshableview.getchildat (0). GetTop () >= Mrefreshableview.gettop ();}} return false;}

This is divided into the current Recyclerview whether the adapter is set, if the data adapter is not set, we are allowed to refresh the load action, so return true; then just get the first entry view,gettop () is to get it away from the parent control ( Recyclerview) The distance from the top, if the distance is equal to Recyclerview distance from the top of the first wood is completely visible, a bit difficult to understand, to draw a diagram illustrated below:


As shown, in the Pulltorefresh analysis of the second, UI structure we know that the structure of the UI to add the LinearLayout header, tail and the middle of the content, in the "Refresh the head", "Loading the tail" is not displayed, In LinearLayout, there is only one framelayout of the middle content of the parent control, in the picture on the right, the first entry view,gettop () gets a negative value that is less than the GetTop () value of Recyclerview (0).    So this time returns false; GetTop () Gets a value of 0, which is equal to Recyclerview's GetTop () value of 0 when the first entry is fully displayed. I don't know, everyone. No, the Getfirstvisiableposition () method is used when getting the first visible item position subscript
/** * @Description: Gets the position of the first visible child view subscript * * @return int: Position * @version 1.0 * @date 2015-9-23 * @Author Zhou.wenkai */private int getfirstvisibleposition () {    View firstvisiblechild = mrefreshableview.getchildat (0);    return firstvisiblechild! = null? Mrefreshableview.getchildadapterposition (Firstvisiblechild):-1;}

6. How to judge to the bottom of this idea and judge the top logic similar.Iv. SummaryThe following is a summary of the process steps for the extended control: 1.    Inherit Pulltorefreshbase; 2. Implement four abstract methods in the parent class; 3. Implement all the constructor methods in the parent class; 4. Set the direction in Getpulltorefreshscrolldirection (); 5. The Createrefreshableview () initializes the refresh load view; 6. The Isreadyforpullstart () determines whether the top of the refresh load view is visible; 7. The Isreadyforpullend () determines whether the bottom of the Refresh load view is visible. Download the source code to provide you with a GitHub address: Android-pulltorefresh Also, welcome star or f**k me on github!Vi. ConclusionIn this article, we figure out how to expand the Pulltorefresh framework, and expand a pulltorefreshrecyclerview, I believe that you can quickly expand in their own development of the desired control, the next article in the "Android Pulltorefresh analysis of the five, extended refresh load Style "We are going to modify the Pulltorefresh framework so that it can gracefully define its own refresh loading style!"

[Go] Android pulltorefresh extension Recyclerview

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.