Commonly used listview and scrollview in android

Source: Internet
Author: User

Directly add the code.

In fact, it mainly uses the OnScrollListener inherited interface to determine whether the listview reaches the last one in the onScrollStateChanged method.

Load the data and the following code.

Public class MainActivity extends Activity implements OnScrollListener {private ArrayAdapter adapter; private ListView; private TextView loadText; private ProgressBar pg; private List
 
  
List; // View private View moreView at the bottom of ListView; private Handler handler = new Handler (); private int MaxNum = 22; // you can specify the maximum number of data entries, if the value is exceeded, the private int lastVisibleIndex of the last visible entry is no longer loaded. @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); listView = (ListView) findViewById (R. id. ll); moreView = getLayoutInflater (). inflate (R. layout. moredata, null); loadText = (TextView) moreView. findViewById (R. id. bt_load); pg = (ProgressBar) moreView. findViewById (R. id. pg); listView. addFooterView (moreView); // use map to load data and initialize 10 data items list = new ArrayList
  
   
(); // Instantiate SimpleAdapter adapter = new ArrayAdapter (this, R. layout. item, R. id. TV _title, list); listView. setAdapter (adapter); listView. setOnScrollListener (this); loadDate ();/** manually load ** // loadText. setOnClickListener (new OnClickListener () {// @ Override // public void onClick (View v) {// pg. setVisibility (View. VISIBLE); // VISIBLE to the progress bar // loadText. setVisibility (View. GONE); // The button is invisible // handler. postDelayed (new Runn Able () {// @ Override // public void run () {// loadDate (); // loadText. setVisibility (View. VISIBLE); // pg. setVisibility (View. GONE); // adapter. notifyDataSetChanged (); // notify listView to refresh data ///} //}, 2000 );//}//});} /** note * onScroll is triggered after sliding * onScrollStateChanged is triggered by dragging and sliding **/@ Override public void onScroll (AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {// calculate the index of the last visible entry LastVisibleIndex = firstVisibleItem + visibleItemCount-1; // if all entries are equal to the maximum number of entries, the bottom View if (totalItemCount = MaxNum + 1) {listView is removed. removeFooterView (moreView); Toast. makeText (this, "data is fully loaded", Toast. LENGTH_LONG ). show () ;}// when sliding, determine whether it has reached the bottom. @ Override public void onScrollStateChanged (AbsListView view, int scrollState) {// OnScrollListener. SCROLL_STATE_TOUCH_SCROLL // OnScrollListener. SCROLL_STATE_FLING // OnScrollListener. SCROLL_STATE_IDLE if (scrollState = OnScrollListener. SCROLL_STATE_IDLE & lastVisibleIndex = adapter. getCount () {// pg is automatically loaded when it slides to the bottom. setVisibility (View. VISIBLE); loadText. setVisibility (View. GONE); handler. postDelayed (new Runnable () {@ Override public void run () {loadDate (); loadText. setVisibility (View. VISIBLE); pg. setVisibility (View. GONE); adapter. notifyDataSetChanged () ;}}, 2000) ;}// load the data private void loadDate () {int count = adapter. getCount (); // if (count + 5 <MaxNum) {// for (int I = count; I <count + 5; I ++) {// list. add ("add row" + I + "); //} else {// for (int I = count; I <MaxNum; I ++) {// list. add ("add row" + I + "); //} // load 10 for (int I = count; I <count + 20; I ++) {list. add ("add row" + I + ");}}}
  
 

Several projects:

1. The above simple pull-up loading more examples

Http://download.csdn.net/detail/kongbaidepao/6884945


2. Online pull-up loading more pull-down refresh examples

Http://download.csdn.net/detail/kongbaidepao/6884933


3. There is also a perfect example of loading listview scrollview gridview.

Http://download.csdn.net/detail/kongbaidepao/6884999


Another open-source project is included. If you are interested, you can check it out.

Https://github.com/chrisbanes/Android-PullToRefresh

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.