Android controls ListView scroll up and down via code

Source: Internet
Author: User

This article describes a way to control how the ListView scrolls up and down through code.

First:


Pressing the button triggers the ListView to scroll or stop.


It is not difficult to implement this function, the following is the main code Mainactivity.java

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 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 voidOnClick (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:listscrolloff (); 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 () {Listscrolloff (); handler.postdelayed (run_scroll_up, 0);} /** * Scroll down */public void Listscrolldown () {Listscrolloff (); handler.postdelayed (Run_scroll_down, 0);} /** * Stop scrolling */public void Listscrolloff () {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 d uration) * * 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); 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 implement the ListView position change.

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:

Android ListView Code controls Move up and down



Android controls ListView scroll up and down via code

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.