Android(安卓)ListView 滚动的N种方法
16lz
2022-03-23
Android 里面让ListView滚动有N种方法,这儿列举三种:
我的需求是通过按键让Listview滚动起来,当然这些按键不是通过Android标识接口传输过来的,所以不能通过监听按键事件来实现此功能;
一、方法一:
也是最常用的方法:
直接跳转到指定的位置,可以是事件每触发一次,执行一次,形成滚动效果;
二、方法二:
smoothScrollToPosition类似于setSelection,但setSelection没有动画效果,直接跳转,smoothScrollToPosition是类似于手滑动,有滚动过程,从名字上就是平滑的意思;
三、方法三:
这个方法比较另类,一般用不到,那就是先让listview获取焦点,然后注入上下按键事件:
我的需求是通过按键让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();}
更多相关文章
- Android解决输入法自动弹出方法大全
- android之视频播放
- Android(安卓)AsyncTask完全解析,带你从源码的角度彻底理解
- Android程序测试(2)——Activity测试
- Android(安卓)显示系统键盘搜索按键,实现搜索功能
- android 弹出日期滑动选择框,日期滚动滑动选择
- Android(安卓)使用Parcelable传递对象
- 浅谈Java中Collections.sort对List排序的两种方法
- Python list sort方法的具体使用