Android ListView Implementation Pull up load more and pull down refresh function _android

Source: Internet
Author: User
Tags gety message queue first row

The example of this article introduces the implementation method and function of the Android ListView down-Pull refresh function, for everyone's reference, the specific contents are as follows

1, ListView Optimization mode

Interface Caching : Viewholder+convertview
page load: pull-up refresh
Picture Caching
Fast sliding listview Disable Refresh

2, the effect

3, pull the load more principles and implementation

When our fingers slide to the last position of the ListView, we trigger the method that loads the data. This triggers before we need to do some work, including:

How to judge slide to the last?

How do I avoid repeating data loading?

How do I refresh the interface after loading?

1, interface to achieve Abslistview.onscrolllistener interface, when the Firstvisibleitem + visibleitemcount >= Totalitemcount can be judged by sliding to the bottom;
2, in order not to repeat the loading of data, for example, when my fingers slide to the bottom, the fingers immediately leave, and then immediately slide, it will cause the data pull two times, we use Mhandler.hasmessages (LOAD) to determine whether the message queue already exists in this message;
3), we load new data in handler, and use Mstringarrayadapter.notifydatasetchanged (), notify interface refresh.

 int result1;
 int result2;
 The listening state changes
 @Override public
 void onscrollstatechanged (abslistview view, int scrollstate) {
 switch ( Scrollstate) {case
 Scroll_state_idle:
 //sliding end break
 ;
 Case scroll_state_fling://
 finger left screen
 if (result1 >= result2) {
  //Send only once to prevent loading data multiple times
  if (! Mhandler.hasmessages (load))
  mhandler.sendemptymessagedelayed (load, 5000);
 }
 break;
 Case Scroll_state_touch_scroll:
 //sliding in break
 ;
 }

 After the slide
 is done @Override public
 void Onscroll (abslistview view, int firstvisibleitem, int visibleitemcount, int Totalitemcount) {
 //LOG.E (TAG, "Firstvisibleitem:" + Firstvisibleitem + "---visibleitemcount:" + visibleitemcount + "--totalitemcount:" + totalitemcount);
 Load to the last
 if (Firstvisibleitem + visibleitemcount >= totalitemcount) {
 ///here does not handle, after the state change at the end and then deal with
 }
 }

4. The principle and realization of down-pulling and refreshing

The drop-down refresh core principle is consistent with the pull-load core principle, but with different details. We need to deal with the following questions: How to determine the user's current position in the data first? How to judge the user to pull a certain distance at the same time in the first article? How do I refresh the update interface (already resolved above)?

1), Attention matters:

Initialization should be set by default above the refresh icon hidden, in the ProgressBar set android:visibility= "Gone"; This setting is not yet, you need to set mlistview.setselection (1); Let the current first column be the first piece of data. Also, set the Mlistview.setselection (1) After the load is complete, and display the first row.

2), to achieve

* The way to judge the first article mlistview.getfirstvisibleposition () ==0
*mlistview implements the Mlistview.setontouchlistener method to calculate the distance between the two and the size of the distance (for example, 100) according to the position of the user's finger and the position raised, such as the refresh operation if greater than.
* Notify the interface mstringarrayadapter.notifydatasetchanged () after refreshing;

 Mlistview.setontouchlistener (New View.ontouchlistener () {
 @Override public
 boolean Ontouch View V, Motionevent event) {

 switch (event.getaction ()) {case
  Motionevent.action_down:
  starty=event.gety ();
  break;
  Case Motionevent.action_move: Break
  ;
  Case MOTIONEVENT.ACTION_UP:
  endy=event.gety ();
  if (endy-starty> distant&& (mlistview.getfirstvisibleposition () ==1| | Mlistview.getfirstvisibleposition () ==0)) {
  if (!mhandler.hasmessages (fresh)) {
  Mprogressbar_ Top.setvisibility (view.visible);
  Notif
  mhandler.sendemptymessagedelayed (fresh,5000);
  }
  break;
 Cannot return true, otherwise direct consumption has been returned
 false;
 

This is the entire content of this article, I hope to learn more about Android software programming help.

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.