Android implementation Pull load more ListView (Pulmlistview) _android

Source: Internet
Author: User
Tags addall

Ideas

Today to bring you to achieve a pull load more Listview.github Portal: Pulmlistview, welcome everyone fork&&star.

First of all, if we want to implement a pull load more listview, we need to implement the features include:
1. A custom ListView, and the ListView is able to determine whether the current is at the bottom.
2. A custom Footerview for UI display during ListView loading process.
3. Associated Footerview and ListView, including timing judgment, Footerview display and concealment.
4. To provide a load more interface, easy to callback users to actually load more functional implementation.
5. Provide a callback method that loads more end to add the user's latest data and update the relevant status tag and UI display.

For the above 5 functions, we analyze the corresponding implementation method.

function 1 (Custom listview)

We can implement a custom Pulmlistview by inheriting ListView.

public class Pulmlistview extends ListView {public
  Pulmlistview (context
    , null);
  Public

  Pulmlistview (context, AttributeSet attrs) {This
    (context, attrs, 0);
  }

  Public Pulmlistview (context, AttributeSet attrs, int defstyleattr) {
    Super (context, attrs, defstyleattr); c9/>//Initialize
    init ();
  }


It is not enough to implement the three constructors of ListView, we need ListView to be able to determine whether the current ListView slides to the last element.

To determine whether to slide to the last element, we can do so by setting the Onscrolllistener for ListView. The code is as follows:

private void Init () {Super.setonscrolllistener (new Onscrolllistener () {@Override public void Onscrollstatecha
        Nged (abslistview view, int scrollstate) {//Invoke user set Onscrolllistener if (Museronscrolllistener!= null) {
      Museronscrolllistener.onscrollstatechanged (view, scrollstate); @Override public void Onscroll (Abslistview view, int firstvisibleitem, int visibleitemcount, int totalite Mcount) {//Invoke user-set Onscrolllistener if (Museronscrolllistener!= null) {Museronscrolllistener.onscro
      ll (view, Firstvisibleitem, VisibleItemCount, Totalitemcount); //Firstvisibleitem is the position of the first element that the current screen can display//VisibleItemCount is the number of elements that the current screen can display//Totalitemcount is the ListView contained
      Total element int lastvisibleitem = Firstvisibleitem + visibleitemcount; if (!misloading &&!mispagefinished && lastvisibleitem = = Totalitemcount) {if (Monpulluploadmorel
    Istener!= null) {misloading = true;      Monpulluploadmorelistener.onpulluploadmore ();
}
      }
    }
  });

 }

From the code comment, you can know that by (Firstvisibleitem + visibleitemcount) You can get the number of elements that are already displayed on the current screen, and if the number of elements already displayed equals the total number of ListView elements, Then you can assume that the ListView has slipped to the bottom.

function 2 (Custom Footerview)

Here we can implement a simpler footerview, that is, to load more UI layouts. For example, we can show a ProgressBar and a line of text, the following code:

/**
 * Loads more view layouts and can be customized.
 * * Public
class Loadmoreview extends LinearLayout {public

  Loadmoreview (context) {
    the context, NULL);

  Public Loadmoreview (context, AttributeSet attrs) {This
    (context, attrs, 0);
  }

  Public Loadmoreview (context, AttributeSet attrs, int defstyleattr) {
    Super (context, attrs, defstyleattr); C14/>init ();
  }

  private void Init () {
    layoutinflater.from (GetContext ()). Inflate (R.layout.lv_load_more, this);
  }


Layout file:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
  android:id=" @+id/id_load_more_layout "
  android:layout_width=" Match_parent "
  android: layout_height= "Wrap_content"
  android:orientation= "horizontal"
  android:gravity= "center"
  android: layout_margin= "@dimen/loading_view_margin_layout" >

  <progressbar
    android:id= "@+id/id_loading_ ProgressBar "
    android:layout_width=" @dimen/loading_view_progress_size "
    android:layout_height=" @dimen Loading_view_progress_size "
    android:indeterminate=" true "
    style="? Android:progressbarstylesmall/>

  <textview
    android:id= "@+id/id_loading_label"
    android:layout_width= "Wrap_content"
    android:layout_height= "Wrap_content"
    android:text= "@string/page_loading"/>
</LinearLayout>

function 3 (Associated ListView and Footerview)

First, we need to save the Footerview in the ListView with a variable and instantiate it in the constructor.

Private View Mloadmoreview;
private void Init () {
  Mloadmoreview = new Loadmoreview (GetContext ());
}

Second, we need to control the display and concealment of Footerview. Consider the timing of the display and concealment of Footerview:
• Time to display: ListView is at the bottom and more data is currently needed to load.
• Hidden timing: ListView finishes loading more operations.

To determine whether there is currently data to be loaded, we need to define a Boolean variable mispagefinished to indicate whether the data load ends.
To ensure that only one data loading process is performed at the same time, we also need to define a Boolean variable misloading to indicate whether the data load state is currently in place.

The Footerview shows and hides the time, also has the control state the variable, the code also relatively easy to realize.

Show Time:

private void Init () {misloading = false;//initialization is not loaded mispagefinished = false;//The default has more data to load when initializing Mloadmoreview = New Loadmoreview (GetContext ()); Instantiate Footerview Super.setonscrolllistener (new Onscrolllistener () {@Override public void onscrollstatechanged (A Bslistview view, int scrollstate) {//Invoke user set Onscrolllistener if (Museronscrolllistener!= null) {MU
      Seronscrolllistener.onscrollstatechanged (view, scrollstate); @Override public void Onscroll (Abslistview view, int firstvisibleitem, int visibleitemcount, int totalite Mcount) {//Invoke user-set Onscrolllistener if (Museronscrolllistener!= null) {Museronscrolllistener.onscro
      ll (view, Firstvisibleitem, VisibleItemCount, Totalitemcount);
      int lastvisibleitem = Firstvisibleitem + visibleitemcount; Performs load more operations if the ListView tail and more data needs to be loaded and there is currently no loader in progress (!misloading &&!mispagefinished && Lastvisibl Eitem = = Totalitemcount) {if (Monpulluploadmorelistener!= null) {misloading = true;//The State is set to True when the load is more in progress SHOWLOADMO ReView (); Show load more layouts Monpulluploadmorelistener.onpulluploadmore ();
Invoke user-Set load more callback interface}}});
  The private void Showloadmoreview () {///here will load more root layout IDs set to Id_load_more_layout, enabling users to load more layouts from custom.
  if (Findviewbyid (r.id.id_load_more_layout) = = null) {Addfooterview (Mloadmoreview);

 }
}

Hidden opportunity:

/**
 * Load More End ListView callback method.
 * *
 * @param ispagefinished paging is over
 * @param newitems    The data that is loaded on the page
 * @param isfirstload  Whether the data is loaded for the first time ( Used to configure the Drop-down refresh framework to avoid page flashes)
 /public
void Onfinishloading (Boolean ispagefinished, List<?> NewItems, Boolean isfirstload) {
  misloading = false;//tag is currently not loaded with more programs executing
  setispagefinished (ispagefinished); Sets whether paging ends the flag and removes Footerview
}

private void Setispagefinished (Boolean ispagefinished) {
  mispagefinished = ispagefinished;
  Removefooterview (Mloadmoreview);
}

function 4 (Pull up the callback interface to load more implementations)

This is relatively simple, we define a interface, it is easy to callback the user really load more implementation methods.

/**
 * Pull load more callback interface/public
interface Onpulluploadmorelistener {
  void Onpulluploadmore ();
}

Private Onpulluploadmorelistener Monpulluploadmorelistener;
/**
 * Settings Pull load more callback interface.
 * @param l Pull load more callback interface/public
void Setonpulluploadmorelistener (Onpulluploadmorelistener l) {
  This.monpulluploadmorelistener = l;
}

function 5 (Loading more end callbacks)

In order to maintain a collection of data in Pulmlistview, you must customize a adapter, use the list in adapter to store data sets, and submit methods for adding and deleting.

Custom adapter:

/**
 * Abstract adapter.
 * * Public
abstract class Pulmbaseadapter<t> extends Baseadapter {
  protected list<t> items;

  Public Pulmbaseadapter () {
    this.items = new arraylist<> ();
  }

  Public Pulmbaseadapter (list<t> items) {
    this.items = items;
  }

  public void Addmoreitems (List<t> newitems, Boolean isfirstload) {
    if (isfirstload) {
      this.items.clear ( );
    }
    This.items.addAll (NewItems);
    Notifydatasetchanged ();
  }

  public void RemoveAllItems () {
    this.items.clear ();
    Notifydatasetchanged ();
  }


Why add a Isfirstload variable to the Addmoreitems method?

It is because the pull load is more often used in conjunction with the Drop-down refresh. The drop-down refresh process involves a ListView data collection clear and then AddAll. If there is no isfirstload parameter, then the user Drop-down Refresh to update the ListView data set must be divided into two steps :
1.removeAllItems and carry out notifydatasetchanged.
2.addMoreItems and carry out notifydatasetchanged.

Two consecutive notifydatasetchanged at the same time can result in a splash screen, so a Isfirstload method is presented here. When the data is first loaded, all the data is clear, then the AddAll, and finally notify.

With a custom adapter, you can write a callback function that loads more ends:

/**
 * Load More End ListView callback method.
 * *
 * @param ispagefinished paging is over
 * @param newitems    The data that is loaded on the page
 * @param isfirstload  Whether the data is loaded for the first time ( Used to configure the Drop-down refresh framework to avoid page flashes)
 /public
void Onfinishloading (Boolean ispagefinished, List<?> NewItems, Boolean isfirstload) {
  misloading = false;
  Setispagefinished (ispagefinished);
  Add updated Data
  if (newitems!= null && newitems.size () > 0) {
    pulmbaseadapter adapter = (pulmbaseadapter ) ((Headerviewlistadapter) Getadapter ()). Getwrappedadapter ();
    Adapter.addmoreitems (NewItems, isfirstload);
  }


It is important to note that after adding footerview or headerview, we cannot get our custom adapter through Listview.getadapter, we must follow the following steps:

Copy Code code as follows:
Pulmbaseadapter adapter = (Pulmbaseadapter) ((Headerviewlistadapter) Getadapter ()). Getwrappedadapter ();

Reference
1.PagingListView

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.