Android in ListView pull-up load more and drop-down refresh

Source: Internet
Author: User

For almost every Android app development, there is a control, which is a ListView that loads multiple data and shows it in a certain style. But in order to performance problems (one-time loading too much data, such as 100,000, consuming long, resource-intensive, etc.) and user experience problems (such as users only want to see the latest 10 data, the results of all the tens of thousands of data are loaded, not easy for users to choose) and other reasons, So we are going to load the data of the ListView, which is commonly used to load more and pull up the latest data in the ListView.

We can encapsulate a ListView with a pull-down function, usually with the head headerview and the bottom footerview, and then through the display of the number of data and the total data bar data to determine whether it can pull up, and then use the corresponding monitoring to achieve up and down refresh operation, There is a lot of information on these websites. Today's code is used in the work, there are open Source Library packaged open source control Pulltorefreshlistview. In fact, the source code is:https://github.com/chrisbanes/Android-PullToRefresh , detailed usage can be viewed.

The use of Pulltorefreshlistview is simple, same as the ListView, the adapter to which the data is provided adapter, and then the Setadapter () and the click events are the same. I put my work in the top and bottom of the refresh implementation of the code is simply posted, make a note:

Privatepulltorefreshlistview Freshlistview;

Privatebeforerecordadapter adapter;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);

Setcontentview (R.layout ...);

freshlistview= (Pulltorefreshlistview) Findviewbyid (R.ID.PERIOD_RECORD_LV);//corresponding pulltorefreshlistview in the page layout file
Freshlistview.setmode (Mode.both);//mode for pull-down

/* 
         * Mode.both: Support pull-down   at the same time;
         *mode.pull_from_start: only supports dropdown pulling down 
         * Mode.pull_from_end: Supports only pull-up pulling up 
         * If mode is set to Mode.both, you need to set refresh listener to OnRefreshListener2 and implement                      onpulldowntorefresh (), Onpulluptorefresh () two methods. &NBSP
         * If mode is set to Mode.pull_from_start or Mode.pull_from_end, You need to set the refresh listener to Onrefreshlistener while implementing the Onrefresh () method.  
         * can of course be set to OnRefreshListener2, but mode.pull_from_ Start only calls the Onpulldowntorefresh () method,  
         * Mode.pull_ Only the Onpulluptorefresh () method is called from.  
         */

Freshlistview.setonrefreshlistener (newonrefreshlistener2<listview> () {
@Override
Public Voidonpulldowntorefresh (pulltorefreshbase<listview> arg0) {

Pull-down Refresh implementation

current_page= 1;//page load, the drop-down is the 1th page of data
                       getData ();//Fetch data from the server
                }

                 @ Override
                 Public Voidonpulluptorefresh (pulltorefreshbase<listview> arg0) {

Pull up load more
current_page++;//page load, pull up to load next page of data
GetData ();//Fetch data from the server
}
});
adapter = new Beforerecordadapter (this);//Adapter
Freshlistview.setadapter (adapter);//Set data
Freshlistview.setonitemclicklistener (Beforeitemclick);//item Click event

}

Load server data, or construct data yourself

Privatevoid GetData () {

The first is to obtain the requested data and calculate the number of data totalsize

Then according to TotalSize to determine which mode Freshlistview should deal with, such as the data is loaded and can no longer pull up refresh

if (TotalSize = = 0) {
Freshlistview.setmode (mode.disabled);
} else if (Adapter.getcount () >= totalsize) {
Freshlistview.setmode (Mode.pull_from_start);
} else {
Freshlistview.setmode (Mode.both);
}
Freshlistview.onrefreshcomplete ();

}

This allows you to simply implement the pull down function, of course, if the original refresh or load style is not satisfied, you can rewrite it!


Android in ListView pull-up load more and drop-down refresh

Related Article

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.