我的需求是通过按键让Listview滚动起来,当然这些按键不是通过Android标识接口传输过来的,所以不能通过监听按键事件来实现此功能;
一、方法一:
也是最常用的方法:
listView.setSelection(position);
直接跳转到指定的位置,可以是事件每触发一次,执行一次,形成滚动效果;
二、方法二:
listView.smoothScrollBy(30, 200); listView.smoothScrollToPosition(index);
smoothScrollToPosition类似于setSelection,但setSelection没有动画效果,直接跳转,smoothScrollToPosition是类似于手滑动,有滚动过程,从名字上就是平滑的意思;
三、方法三:
这个方法比较另类,一般用不到,那就是先让listview获取焦点,然后注入上下按键事件:
void sendKey(final int key) { new Thread() { public void run() { try { Instrumentation inst = new Instrumentation(); inst.sendKeyDownUpSync(key); } catch (Exception e) { e.printStackTrace(); } } }.start(); }