Pull-down refresh principle ., Pull-down refresh Principle

Source: Internet
Author: User

Pull-down refresh principle ., Pull-down refresh Principle

1 package com. lixu. listviewrefresh; 2 3 import java. util. arrayList; 4 5 import android. app. activity; 6 import android. OS. bundle; 7 import android. widget. absListView; 8 import android. widget. absListView. onScrollListener; 9 import android. widget. arrayAdapter; 10 import android. widget. listView; 11 12 public class MainActivity extends Activity {13 private ArrayList <String> data; 14 private ArrayAdapter <String> mMyAdapter; 15 16 @ Override17 protected void onCreate (Bundle savedInstanceState) {18 super. onCreate (savedInstanceState); 19 setContentView (R. layout. activity_main); 20 21 data = new ArrayList <String> (); 22 23 for (int I = 0; I <30; I ++) {24 data. add ("Test Data" + I); 25} 26 27 ListView lv = (ListView) findViewById (R. id. listview); 28 29 mMyAdapter = new ArrayAdapter <String> (this, android. r. layout. simple_list_item_1, data); 30 31 lv. setAdapter (mMyAdapter); 32 33 lv. setOnScrollListener (new OnScrollListener () {34 private int firstVisibleItem; 35 private int visibleItemCount; 36 private int totalItemCount; 37 38 @ override 39 public void onScrollStateChanged (AbsListView view, int scrollState) {40 // judge 41 if (OnScrollListener when sliding stops. SCROLL_STATE_IDLE = scrollState) {42 43 if (firstVisibleItem = 0) {44 45 data. add (0, "add header data" + (totalItemCount + 1); 46 47 mMyAdapter. notifyDataSetChanged (); 48 49} 50} 51 52 if (OnScrollListener. SCROLL_STATE_IDLE = scrollState) {53 54 if (firstVisibleItem + visibleItemCount) = totalItemCount) {55 56 data. add ("add tail data" + (totalItemCount + 1); 57 58 mMyAdapter. notifyDataSetChanged (); 59 60} 61} 62 63} 64 65 @ Override66 public void onScroll (AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {67 68 this. firstVisibleItem = firstVisibleItem; 69 this. visibleItemCount = visibleItemCount; 70 this. totalItemCount = totalItemCount; 71 72} 73}); 74 75} 76 77}

:

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.