Listview paging Loading

Source: Internet
Author: User

When too much data needs to be displayed on the listview, all the data will be loaded at once, and the interface will be stuck, which will affect the user experience. In this case, the loading should be completed several times.

To implement this function, an onscrolllistener interface and a footview layout file are required.
1. The layout file of footview is very simple, which indicates loading.

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: gravity = "center_horizontal" Android: orientation = "vertical"> <textview Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: text = "load more"/> </linearlayout>

At this time, when I call listview. removefootview (View);, an error occurs. After finding the cause, I finally understand

Before using the set adapter class, you must first use listview. addfooterview (footerview );

Because listview creates different adapter classes based on whether footview exists to view the source code

public void setAdapter(ListAdapter adapter) {        if (mHeaderViewInfos.size() > 0|| mFooterViewInfos.size() > 0) {            mAdapter = new HeaderViewListAdapter(mHeaderViewInfos, mFooterViewInfos, adapter);        } else {            mAdapter = adapter;        }}

No addfootview is provided before the set method. If removefootview is called, a class Exception error will be reported.
2. Create a sliding listener class

Private class implements detail {private Boolean addflag; @ overridepublic void detail (abslistview view, int scrollstate) {}@ overridepublic void onscroll (abslistview view, int detail, int visibleitemcount, int totalitemcount) {If (totalitemcount = visibleitemcount + firstvisibleitem & firstvisibleitem! = 0 &&! Addflag) {// todo load the new data item // if there is data, addflag = true; so that the next time you can enter the if statement to load the listview. setselection (totalItemCount-1); // display the new add-on in listview }}}

According to the practice, you do not need to add footview because data is pulled from the database because it is fast and can hardly be seen.

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.