The diycode open source project instance builds fragments for pull-up loading and pull-down refresh
The following explains the implementation of the fragment of pull-up loading and pull-down refresh through three knowledge points, and introduces the usage of each knowledge point.
1. Effect preview
1.1. This home page is a fragment, and this article describes the construction method of this fragment.
The pull-down will have a rotating refresh circle, and the pull-up will refresh the data.
1.2. Overall structure
First, the bottom layer is basefragment
Then refreshrecyclerfragment inherits basefragment
Then simplerefreshrecyclefragment inherits the refreshrecyclerfragment
So if you want to apply classes, you just need to inherit simplerefreshrecyclefragment.
2. The most basic basefragment
2.1. Let's first look at the member variables
Viewholder as view manager
Config is used as configuration status information
Diycode is an online server of the project
Datacache is a buffer
2.2.oncreate function to complete initialization
Config is to get the singleton.
Diycode is also a singleton.
Datacache is a new class.
2.3. Define the abstract function to obtain the layout ID
This function will be implemented in the successor.
2.4. Obtain viewholder in this class
2.5. Implement oncreateview
The newly created viewholder actually uses a getlayouteid () method, which calls the abstract function of this class.
Then a view is returned.
2.6. Implement the abstract function initviews
2.7. Copying onactivitycreated
Implements the indirect call to abstract functions.
2.8. Prompt function toast
2.9. Life cycle of a fragment
Therefore, the execution order here is:
onCreate==>onCreateView==>onActivityCreate
Oncreate function: initialize configuration and create diycode singleton and data buffer.
Oncreateview function: get viewholder.
Onactivitycreate function: implement an abstract function initviews, which is used to implement the base class.
3. Fragment with pull-down refresh and pull-up loading