Android uses code to control ListView Scrolling up and down

Source: Internet
Author: User

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



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.