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