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

2023-09-14 11:19:03

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.CoordinatorLayout
    xmlns: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.NestedScrollView
        android:id="@+id/main_content_scrollable_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="com.android.settings.widget.FloatingAppBarScrollingViewBehavior"> 

        <LinearLayout
            android:id="@+id/homepage_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <FrameLayout
                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"/>

            <FrameLayout
                android: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.AppBarLayout
        android: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.CoordinatorLayout
    xmlns: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.NestedScrollView
        android:id="@+id/main_content_scrollable_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="com.android.settings.widget.FloatingAppBarScrollingViewBehavior"> -->

        <LinearLayout
            android:visibility="gone"
            android:id="@+id/homepage_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"/>

            <FrameLayout
                android: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"/>

            <FrameLayout
                android: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.AppBarLayout
        android: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>

更多推荐

【Docker】ubuntu20.04 X86机器搭建NVIDIA ARM64 TX2的Docker镜像

文章目录1.设置ubuntu为清华源1.1备份源文件1.2替换清华源1.3更新清华源2.UbuntuDocker安装3.安装qemu4.安装NvidiaTX2Docker镜像5.如何使用TX2容器6.参考资料1.设置ubuntu为清华源为了后面ubuntu下载安装软件快些,需要使用国内的源,比如清华源。(备注:已经更新

解读《生成式人工智能服务管理暂行办法》

《生成式人工智能服务管理暂行办法》第一章总则第二章技术发展与治理第三章服务规范第四章监督检查和法律责任第五章附则以ChatGPT为代表的现象级互联网应用的出现,掀起了人工智能领域新一轮技术浪潮。作为新一代信息技术,生成式人工智能通过对人类“脑力”的无限延伸,赋予人们对于追求美好生活的更大想象空间。与此同时,生成式人工智

云原生之使用Docker部署Firefox浏览器

云原生之使用Docker部署Firefox浏览器一、Firefox浏览器介绍1.1Firefox简介1.2Firefox特点二、本次实践介绍2.1本地环境规划2.2本次实践简介三、本地环境检查3.1检查Docker服务状态3.2检查Docker版本3.3检查dockercompose版本四、下载Firefox镜像五、部

QCustomPlot绘图类详解(大白话)

本文假定你会使用Qt开发,但未接触过QCustomPlot绘图类或者是刚接触。如何往Qt中引入QCustomPlot首先,去官网下载最新版本的源码,注意是QCustomPlot.tar.gz这个文件,里面包含源码和示例。实际上,我们只需要qcustomplot.h和qcustomplot.h这两个源文件。将代码文件拷贝

设计模式:简单工厂、工厂方法、抽象工厂

参考Java设计模式之创建型:工厂模式详解(简单工厂+工厂方法+抽象工厂)-知乎工厂方法以生产手机为例,具体的UML图如下:这种方法的优点是对于用户来说,不再需要面对具体的生产逻辑,只需要将生产的安排工作和细节都交给工厂类去做。缺点是在这种情况下,如果想要增加一种商品,比如华为手机,不仅需要新增一个实现抽象产品的接口,

MYSQL性能优化——SQL 性能分析

SQL性能分析⭐执行计划实际项目开发中,由于我们不知道实际查询的时候数据库里发生了什么事情,数据库软件是怎样扫描表、怎样使用索引的,因此,我们能感知到的就只有sql语句运行的时间,在数据规模不大时,查询是瞬间的,因此,在写sql语句的时候就很少考虑到性能的问题。但是随着数据规模增大,如千万、亿的时候,我们运行同样的sq

opencv形状目标检测

1.圆形检测OpenCV图像处理中“找圆技术”的使用-图像处理-双翌视觉OpenCV图像处理中“找圆技术”的使用,图像处理,双翌视觉https://www.shuangyi-tech.com/news_224.htmlopencv找圆心得,模板匹配比霍夫圆心好用-知乎1相比较霍夫找直线算法,霍夫找圆心算法极其复杂现在还

kubesphere中间件部署

微服务部署前中间件部署一、MySQL部署1.1使用Docker实现MySQL主从复制dockerrun-p3307:3306--namemysql-master\-v/mydata/mysql/master/log:/var/log/mysql\-v/mydata/mysql/master/data:/var/lib/

家居设计软件Live Home 3D Pro mac中文版特点介绍

LiveHome3DPromac是一款专业的3D家居设计软件,可以帮助用户轻松创建和设计家居平面图和3D模型,并进行渲染和虚拟漫游。​​​​​​​​LiveHome3DPromac软件特点1.界面友好:LiveHome3DPro的界面友好,操作简单方便,即使是初学者也可以轻松使用。2.支持多平台:LiveHome3DP

C++ STL库 list(链表)

C++Lists(链表)赋值(assign)语法:voidassign(input_iteratorstart,input_iteratorend);voidassign(size_typenum,constTYPE&val);assign()函数以迭代器start和end指示的范围为list赋值或者为list赋值nu

HTML5 游戏开发实战 | 黑白棋

黑白棋,又叫反棋(Reversi)、奥赛罗棋(Othello)、苹果棋、翻转棋。黑白棋在西方和日本很流行。游戏通过相互翻转对方的棋子,最后以棋盘上谁的棋子多来判断胜负。黑白棋的棋盘是一个有8×8方格的棋盘。开始时在棋盘正中有两白两黑四个棋子交叉放置,黑棋总是先下子。(1)下子规则。把自己颜色的棋子放在棋盘的空格上,而当

热文推荐