Android inside Let the listview scroll there are n methods, here are listed three kinds:
My requirement is to let the ListView scroll by pressing the button, of course, these keys are not transmitted through the Android identity interface, so it is not possible to monitor the key events to achieve this function;
One, method one:
is also the most commonly used method:
Java code
- Listview.setselection (position);
Jump directly to the specified location, can be the event every time the trigger, execute once, to form a scrolling effect;
Two, method two:
Java code
- Listview.smoothscrollby ( 200);
- Listview.smoothscrolltoposition (index);
Smoothscrolltoposition similar to setselection, but setselection no animation effect, direct jump, smoothscrolltoposition is similar to hand sliding, there is a scrolling process, from the name is smooth meaning ;
Three, method three:
This method is more alternative, generally not used, that is, first let the ListView get the focus, and then inject the upper and lower key events:
Java code
- void Sendkey (final int key) {
- New Thread () {
- public Void Run () {
- try {
- Instrumentation Inst = new Instrumentation ();
- Inst.sendkeydownupsync (key);
- } catch (Exception e) {
- E.printstacktrace ();
- }
- }
- }.start ();
- }
Android ListView scrolling N Ways