Android 修复在 Settings 首页,按键盘方向键逐个单选

Android 修复在 Settings 首页,按键盘方向键逐个单选

  • 问题现象
  • 问题分析
  • 解决办法

问题现象

在 Settings 主界面,按键盘方向键上下会直接整个选中,无法单条选中变色,而在二级页面中按方向键上下是正常的。

没有遥控器可以通过 adb 指令模拟下键

adb shell input keyevent 20

问题分析

Settings 中都是用的 Preference 控件来显示界面的,既然二级页面可以,主界面不行的话,那应该是主界面布局有问题。

盲猜和焦点占用有关系,找到主界面布局文件 settings_homepage_container.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:id="@+id/settings_homepage_container"android:fitsSystemWindows="true"android:layout_width="match_parent"android:layout_height="match_parent"><androidx.core.widget.NestedScrollViewandroid:id="@+id/main_content_scrollable_container"android:layout_width="match_parent"android:layout_height="match_parent"app:layout_behavior="com.android.settings.widget.FloatingAppBarScrollingViewBehavior"> <LinearLayoutandroid:id="@+id/homepage_container"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><FrameLayoutandroid:id="@+id/contextual_cards_content"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginStart="@dimen/contextual_card_side_margin"android:layout_marginEnd="@dimen/contextual_card_side_margin"/><FrameLayoutandroid:id="@+id/main_content"android:layout_width="match_parent"android:layout_height="wrap_content"android:animateLayoutChanges="true"android:background="?android:attr/windowBackground"/></LinearLayout></androidx.core.widget.NestedScrollView><com.google.android.material.appbar.AppBarLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:touchscreenBlocksFocus="false"android:keyboardNavigationCluster="false"><include layout="@layout/search_bar"/></com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

可以看到主界面加载的 Preference 最终都是在 main_content 中

层级结构
NestedScrollView
LinearLayout
FrameLayout
Preference

经过验证发现,焦点被最外层 NestedScrollView 处理了,无法传递到 Preference 中,所以就无法单条选中

分析了下 PreferenceScreen 本身就具有屏幕显示不全时可滚动的机制,

谷歌在这最外面又包了一个 NestedScrollView 岂不是多此一举,其实不是这样的,是因为里面还有其它数据,

看到 contextual_cards_content 这个,是用来装 Suggestion 菜单的,为了整体能够滚动,所以套了一个 NestedScrollView

为了事件能过直接被 PreferenceScreen 捕获,

参考了这篇 https://blog.51cto.com/u_15073486/5363888 发现没啥用,而且还浪费时间。。

一开始尝试了通过事件传递回调的方式,试了好几种发现都不行(如果你能成功可以反馈给我),

后来直接将外层的 NestedScrollView 和 LinearLayout 干掉,

这样做虽然可以让主界面单条选中,但弊端是牺牲了 Suggestion 功能。

解决办法

干掉 NestedScrollView,将 homepage_container 和 contextual_cards_content 都设置成 gone,避免编译报错

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:id="@+id/settings_homepage_container"android:fitsSystemWindows="true"android:layout_width="match_parent"android:layout_height="match_parent"><!--     <androidx.core.widget.NestedScrollViewandroid:id="@+id/main_content_scrollable_container"android:layout_width="match_parent"android:layout_height="match_parent"app:layout_behavior="com.android.settings.widget.FloatingAppBarScrollingViewBehavior"> --><LinearLayoutandroid:visibility="gone"android:id="@+id/homepage_container"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"/><FrameLayoutandroid:visibility="gone"android:id="@+id/contextual_cards_content"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginStart="@dimen/contextual_card_side_margin"android:layout_marginEnd="@dimen/contextual_card_side_margin"/><FrameLayoutandroid:id="@+id/main_content"android:layout_width="match_parent"android:layout_height="wrap_content"android:animateLayoutChanges="true"android:background="?android:attr/windowBackground"/><!--  </LinearLayout></androidx.core.widget.NestedScrollView> --><com.google.android.material.appbar.AppBarLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:touchscreenBlocksFocus="false"android:keyboardNavigationCluster="false"><include layout="@layout/search_bar"/></com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/80832.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

flink的几种常见的执行模式

背景 在运行flink时&#xff0c;我们经常会有几种不同的执行模式&#xff0c;比如在IDE中启动时&#xff0c;通过提交到YARN上&#xff0c;还有通过Kebernates启动时&#xff0c;本文就来记录一下这几种模式 flink的几种执行模式 flink嵌入式模式&#xff1a; 这是一种我们在…

GDAL库学习

GDAL库学习 GDAL是一个操作栅格数据和矢量数据的库&#xff0c;对图像而言&#xff0c;可以进行包括读取、写入、转换、处理各种操作。 文章目录 GDAL库学习RasterIO()函数1. 添加引用2. 读取图像3. 获取图像基本信息4. 保存输出图像5. 释放 RasterIO()函数 RasterIO(GDALRWF…

动态的中秋爱心演示送女友用python生成爱心软件文末附c++语言写法

用python生成爱心软件 用python生成动态爱心软件 目录 用python生成爱心软件 完整代码 代码解释 逐句解释 效果展示&#xff1a; 如何打包 c写法 完整代码 import turtledef draw_heart():love turtle.Turtle()love.getscreen().bgcolor("black")love.…

WebSocket vs SSE: 实时数据推送到前端的选择与实现(详细)

Websocket和Server-Sent Events 对比推送数据给前端及各自的实现 二者对比WebSocket&#xff1a;Server-Sent Events (SSE)&#xff1a;选择 WebSocket 还是 SSE&#xff1a; Websocket 实现使用原生 WebSocket API&#xff1a;使用 Netty 创建 WebSocket&#xff1a;总结和选择…

SVN 和 GIT 命令对比

参考 https://blog.csdn.net/justry_deng/article/details/82259470 # TortoiseSVN打分支、合并分支、切换分支 https://www.huliujia.com/blog/802a64152bbbe877c95c84ef2fdf3857a056b536/ # 版本控制&#xff1a;Git与Svn的命令对应关系 TortoiseSVN打分支、合并分支、切换…

达梦数据库修改最大连接数

达梦数据库修改最大连接数 推荐配置修改最大连接数INI参数管理 推荐配置 达梦数据库的最大连接数推荐配置与服务器内存有关。不应设置得过大&#xff0c;否则容易导致出现OOM。 对应关系大致如下&#xff1a; 服务器内存MAX_SESSIONS范围8G50~10016G100~20032G200~30064G300…

unity 实现双击物体让其隐藏,单击物体让其显示

unity 实现双击物体让其隐藏&#xff0c;单击物体让其显示 private float tapThreshold 0.25f; private float tapTimer 0.0f; private bool tap false; private void Update() { if (Input.GetMouseButtonDown(0)) { if (Time.time < this.tapTimer this.tapThreshold)…

【深度学习-注意力机制attention 在seq2seq中应用】

注意力机制 为什么需要注意力机制attention机制的架构总体设计一、attention本身实现评分函数 attention在网络模型的应用-Bahdanau 注意力加性注意力代码实现 为什么需要注意力机制 这是一个普通的seq2seq结构&#xff0c;用以实现机器对话&#xff0c;Encoder需要把一个输入的…

初识React.js

引言&#xff1a; React是一种流行的JavaScript库&#xff0c;用于构建用户界面。无论您是新手还是有一些前端开发经验&#xff0c;本文将带您了解React框架的学习过程&#xff0c;并通过具体的例子来帮助您更好地理解和应用React。 1. 背景起源&#xff1a; React是由Faceb…

Selenium+python怎么搭建自动化测试框架、执行自动化测试用例、生成自动化测试报告、发送测试报告邮件

本人在网上查找了很多做自动化的教程和实例&#xff0c;偶然的一个机会接触到了selenium&#xff0c;觉得非常好用。后来就在网上查阅各种selenium的教程&#xff0c;但是网上的东西真的是太多了&#xff0c;以至于很多东西参考完后无法系统的学习和应用。 以下整理的只是书中…

5.11.Webrtc接口的设计原理

在上节课中呢&#xff0c;我向你介绍了web rtc的接口宏&#xff0c;那有很多同学会产生疑问啊&#xff0c;那觉得web rtc为什么要把接口设计的这么复杂&#xff1f;还非要通过宏来实现一个代理类&#xff0c;再通过代理类来调用到web rtc内部。 那为什么要这么设计呢&#xf…

java微服务项目整合skywalking链路追踪框架

skywalking官网网址&#xff1a;Apache SkyWalking 目录 1、安装skywalking 2、微服务接入skywalking 3、skywalking数据持久化 1、安装skywalking 下载skywalking&#xff0c;本篇文章使用的skywalking版本是8.5.0 Index of /dist/skywalkinghttps://archive.apache.org/…

Net跨平台UI框架Avalonia入门-样式详解

设计器的使用 设计器预览 在window和usercontrol中&#xff0c;在代码中修改了控件&#xff0c;代码正确情况下&#xff0c;设计器中就可以实时看到变化&#xff0c;但是在样式&#xff08;Styles&#xff09;文件中&#xff0c;无法直接看到&#xff0c;需要使用设计器预览D…

Python新手入门

文章目录 概要python代码运行结果小结 概要 以下内容为python各种输出语句的语法&#xff01; python代码 # 标准化输出 print("这是标准化输出&#xff01;")# 格式化输出 print("这是第1种%s"%"格式化输出&#xff01;") print("这是第…

python使用execjs利用jsdom来执行含有document的js代码方案(上)

先说一下环境&#xff1a;win7 64位&#xff0c;python3.8.10。 python使用execjs执行含有document的js代码时&#xff0c;会出现报错&#xff1a; execjs._exceptions.ProgramError: TypeError: document 未定义 原因是纯js代码中是不包含浏览器里对象的(如document、windo…

python 调用adb shell

目录 python调用 bat&#xff0c;启动新窗口&#xff0c;但是不能自动在进入shell 后执行提前设置的操作。 python启动cmd新窗口&#xff0c;但是不能自动在进入shell 后执行提前设置的操作。 python调用 bat&#xff0c;启动新窗口&#xff0c;但是不能自动在进入shell 后执…

Linux--信号

对于 linux来说&#xff0c;信号是软中断。许多重要的程序都需要处理信号。信号&#xff0c;为linux提供了一种处理异步事件的方法。比如&#xff0c;终端用户输入了ctrlc来中断程序&#xff0c;会通过信号机制停止一个程序。 一、信号概述&#xff1a; 1、信号的名字和编号&a…

安装配置 hbase

目录 一 准备并解压hbase安装包 二 修改hbase-env.sh文件 三 修改hbase-site.xml 四 修改环境变量 五 启动和关闭hbase 六 简单使用 hbase 一 准备并解压hbase安装包 这里有网盘资源 链接: https://pan.baidu.com/s/1PuqLmullK70ngeVs6G2oRQ?pwdq93v 提取码: q93v 这里安…

Leetcode165. 比较版本号

力扣&#xff08;LeetCode&#xff09;官网 - 全球极客挚爱的技术成长平台 代码如下&#xff1a; class Solution {public int compareVersion(String version1, String version2) {int i 0, j 0;while(i < version1.length() || j < version2.length()){long num1 0…

endnotes插入文献突然变得格式不对,而且也不是按照正常的顺序来插入解决办法

今天插入文献突然变成了endnotes里面的文献序号&#xff0c;而且也不导入了&#xff0c;多了作者和序号信息 解决办法&#xff1a; 更新一下&#xff0c;然后在进行的导入就ok了&#xff0c;能够按照以前的格式插入了&#xff0c;序号也能自动排开&#xff0c;而且也能导入文献…