Android uses code to control ListView Scrolling up and down
This article introduces a method to control the up and down scrolling of ListView through code.
First:
Pressing the button triggers the ListView to scroll or stop.
It is not difficult to implement this function. The main code MainActivity. java is given below
Package cn. guet. levide; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. listView; public class MainActivity extends Activity implements OnClickListener {private Button btn_up, btn_down, btn_stop; // three buttons private ListView; private Adapter adapter; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); findBy (); init ();} private void init () {btn_up.setOnClickListener (this); btn_down.setOnClickListener (this); btn_stop.setOnClickListener (this ); adapter = new Adapter (this); listview. setAdapter (adapter);} private void findBy () {btn_up = (Button) findViewById (R. id. btn_scroll_up); btn_down = (Button) findViewById (R. id. btn_scroll_down); btn_stop = (Button) findViewById (R. id. btn_scroll_stop); listview = (ListView) findViewById (R. id. listview) ;}@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. btn_scroll_down: listScrollDown (); break; case R. id. btn_scroll_up: listScrollUp (); break; case R. id. btn_scroll_stop: listscroloff (); break ;}} Handler handler = new Handler () {@ Overridepublic void handleMessage (Message msg) {handler. removeCallbacks (run_scroll_down); handler. removeCallbacks (run_scroll_up) ;}};/*** scroll up */public void listScrollUp () {listscroloff (); handler. postDelayed (run_scroll_up, 0);}/*** scroll down */public void listScrollDown () {listscroloff (); handler. postDelayed (run_scroll_down, 0);}/*** stop rolling */public void listscroloff () {handler. removeCallbacks (run_scroll_down); handler. removeCallbacks (run_scroll_up);} Runnable run_scroll_up = new Runnable () {@ Overridepublic void run () {/*** public void smoothScrollBy (int distance, int duration) ** Added in API level 8 Smoothly scroll by distance pixels over duration milliseconds. ** Parameters * distance Distance to scroll in pixels. * duration Duration of the scroll animation in milliseconds. */listview. smoothScrollBy (1, 10); handler. postDelayed (run_scroll_up, 10) ;}}; Runnable run_scroll_down = new Runnable () {@ Overridepublic void run () {listview. smoothScrollBy (-1, 10); handler. postDelayed (run_scroll_down, 10 );}};}
The smoothScrollBy method is used to change the position of the ListView.
public void smoothScrollBy (int distance, int duration) Smoothly scroll by distance pixels over duration milliseconds.Parameters distance Distance to scroll in pixels. duration Duration of the scroll animation in milliseconds.
Project source code:
Android listview code control move up and down