Android ScrollView 不在最顶部问题
Android About 724 words场景
开发中遇到了这样的一个问题,界面最外层是ScrollView,然后里面有嵌套了一个ListView还有其他可以获取焦点的View,然后每次打开界面都会自动滚动到最底部,经过一番折腾,发现了一个简单的方法:
方法1
获取ScrollView里面一个上层任意view,然后调用如下方法:
view.setFocusable(true);
view.setFocusableInTouchMode(true);
view.requestFocus();
方法2(推荐)
在xml中配置,注意,是给ScrollView的子View配置
android:focusable="true"
android:focusableInTouchMode="true"
方法3
scrollView.fullScroll(ScrollView.FOCUS_DOWN);滚动到底部
scrollView.fullScroll(ScrollView.FOCUS_UP);滚动到顶部
需要注意的是,该方法不能直接被调用。
因为Android很多函数都是基于消息队列来同步,所以需要一部操作,addView完之后,不等于马上就会显示,而是在队列中等待处理,虽然很快,但是如果立即调用fullScroll,view可能还没有显示出来,所以会失败。 应该通过handler在新线程中更新
handler.post(new Runnable() {
@Override
public void run() {
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
});
Views: 4,792 · Posted: 2019-04-09
———         Thanks for Reading         ———
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓
Prev Post
Next Post
Today In History
- Telepresence 开发中的常用命令 Views: 2,399 · Posted: 2023-04-08
- Java 中的锁 ReentrantReadWriteLock 读写锁 Views: 2,891 · Posted: 2021-04-08
- Spring Boot 打包本地 jar 包到 war 包中 Views: 2,935 · Posted: 2020-04-08
- Android adb 命令 Views: 6,893 · Posted: 2019-04-08
- JavaScript 页面切换后更改标题 Views: 2,767 · Posted: 2019-04-08
- IDEA 滚轮调节字体大小 Views: 4,432 · Posted: 2019-04-08
- IDEA 去除 Unchecked cast from Object to 提示 Views: 5,537 · Posted: 2019-04-08
- Java 按字典顺序排序请求参数 Views: 7,249 · Posted: 2019-04-08
- Java 储存相同 key 的 map Views: 4,954 · Posted: 2019-04-08
- Java 测试迭代集合耗时 Views: 4,930 · Posted: 2019-04-08
- The temporary upload location [xxxRoot] is not valid Views: 4,339 · Posted: 2019-04-08
- Spring Boot Undertow 上传文件后没有可读权限 Views: 7,804 · Posted: 2019-04-08
- Java8 @FunctionalInterface 注解 Views: 3,136 · Posted: 2019-04-08
- Vue-cli+webpack 配置接口代理解决跨域问题 Views: 4,199 · Posted: 2019-04-08
- PHP 序列化与反序列化 Views: 3,633 · Posted: 2019-04-08
- PHP 关闭 X-Powered-By 信息 Views: 3,000 · Posted: 2019-04-08
- PHP编译安装redis扩展 Views: 3,658 · Posted: 2019-04-08
- PHP Markdown 解析器 Views: 4,152 · Posted: 2019-04-08
- PHP查看配置文件所在位置 Views: 3,454 · Posted: 2019-04-08
- PHP 压缩 HTML 代码 Views: 3,669 · Posted: 2019-04-08
- PhpStorm 设置大括号不换行 Views: 10,034 · Posted: 2019-04-08
- PHP 时间加减 Views: 4,003 · Posted: 2019-04-08
- PHP curl 报错:SSL connect error Views: 4,710 · Posted: 2019-04-08
- PHP 字段对应 MySQL 字段 Views: 3,344 · Posted: 2019-04-08
- ThinkPHP5 设置 Redis 长连接 Views: 7,024 · Posted: 2019-04-08
- ThinkPHP5 设置数据库长连接 Views: 6,896 · Posted: 2019-04-08
- PHP 生成重复固定随机数邀请码 Views: 4,370 · Posted: 2019-04-08
- PHP 使用 json_encode 格式化下标不为 0 开始的数组会转为对象 Views: 4,616 · Posted: 2019-04-08
- PHP 获取毫秒值时间戳 Views: 5,616 · Posted: 2019-04-08
- PHP 设置 json_encode 不转义中文 Views: 4,185 · Posted: 2019-04-08
- PHP 判断函数、类、方法、属性、数组中的 key 是否存在 Views: 6,239 · Posted: 2019-04-08
- Redis 提示没有权限 Views: 6,188 · Posted: 2019-04-08
- Redis 线上禁用命令 Views: 4,263 · Posted: 2019-04-08
- Redis 最大内存策略 Views: 3,030 · Posted: 2019-04-08
- Redis 内存信息 Views: 3,872 · Posted: 2019-04-08
- Redis 使用 bigkeys 命令查找大 key Views: 6,764 · Posted: 2019-04-08
- Redis 运行统计信息 Views: 3,536 · Posted: 2019-04-08
- Redis 实现统计在线人数、每日签到功能 Views: 7,129 · Posted: 2019-04-08
- Redis 使用 HyperLogLog 统计独立 IP 数 Views: 4,922 · Posted: 2019-04-08
- Redis 监控 monitor Views: 4,395 · Posted: 2019-04-08
- Redis 过期时间问题 Views: 3,817 · Posted: 2019-04-08
- Redis 查看连接数 Views: 4,704 · Posted: 2019-04-08
Browsing
Refresh
Loading...
- RabbitMQ 更换日志和数据存储目录 Views: 5577
- Spring Boot Thymeleaf 格式数字时去除千分位逗号分隔符 Views: 323
- MySQL 之关键字和保留字 Views: 3238
- OpenResty 中模板渲染引擎 lua-resty-template Views: 8403
- Next.js App Router 设置统一错误页面 Views: 574
- GoJS Diagram 去掉选中黑色边框 Views: 4314
- HTML 使用 tabindex 控制 Tab 键按键顺序 Views: 2395
- JavaScript 获取 CSS 中定义的变量 Views: 824
- Session Hijacking 会话劫持和 Session Fixation 会话固定 Views: 2251
- CSS 垂直水平居中 Views: 1032