Android detects where the ListView is scrolled to

Source: Internet
Author: User

ListView Scrolling

1. To use a listener event is: Setonscrolllistener ();

The API explanation is:

Set the listener that would receive notifications every time the list scrolls.

Parameters:l the scroll listener

2. It contains two methods:
One is: onscrollstatechanged (abslistview view, int scrollstate), used to indicate what state the ListView is (stationary, sliding);
Scrollstate to Onscrolllistener.scroll_state_idle: Indicates that the ListView is in a stationary state
For Onscrolllistener.scroll_state_touch_scroll: Indicates that the finger has not left the screen while sliding.
The other is: Onscroll (abslistview view, int firstvisibleitem,int visibleitemcount, int totalitemcount) to get some parameters when scrolling:
This is illustrated by a small example:
1  Public classMainactivityextendsActivity {2     PrivateListView ListView;3 4 @Override5     protected voidonCreate (Bundle savedinstancestate) {6         Super. OnCreate (savedinstancestate);7 Setcontentview (r.layout.activity_main);8ListView =(ListView) Findviewbyid (R.id.lsitview);9list<string> list =NewArraylist<string>();Ten          for(inti = 0; I < 20; i++) { OneList.add ("This is the first" + i + "a"); A  -         } -arrayadapter<string> adapter =NewArrayadapter<string> ( This, the Android. R.layout.simple_list_item_single_choice, list); - Listview.setadapter (adapter); -Listview.setonscrolllistener (NewListview.onscrolllistener () { -  + @Override -              Public voidOnscrollstatechanged (Abslistview view,intscrollstate) { +                 Switch(scrollstate) { A  at                  CaseOnscrolllistener.scroll_state_idle://when there's no scrolling -System.out.println ("Last displayed ListView location is--->>" -+listview.getlastvisibleposition ()); -                     if(listview.getlastvisibleposition () = =(ListView -. GetCount ()-1)) { -Toast.maketext (mainactivity. This, "slipped to the bottom of the listview.", in toast.length_short). Show (); -  to                     } +  -                     if(listview.getfirstvisibleposition () = = 0) { theToast.maketext (mainactivity. This, "The ListView is head Up", * toast.length_short). Show (); $                     }Panax Notoginseng  -                      Break; the  +                  Caseonscrolllistener.scroll_state_fling: ASystem.out.println ("Scroll_state_fling"); the                      Break; +  -                  CaseOnscrolllistener.scroll_state_touch_scroll: $System.out.println ("Scroll_state_touch_scroll" $+ "Can see the last displayed location--->>" -+listview.getlastvisibleposition ()); -                      Break; the                 default: - Wuyi                      Break; the  -                 } Wu  -             } About  $             /** - * View the view whose scroll state is being reported - * Firstvisibleitem The index of the first visible cell (ignore if - * VisibleItemCount = = 0) VisibleItemCount the number of visible A * Cells Totalitemcount The number of items in the list adaptor +              */ the @Override -              Public voidOnscroll (Abslistview view,intFirstvisibleitem, $                     intVisibleItemCount,intTotalitemcount) { the  the             } the         }); the  -     } in}

Source:
Source

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.