蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行离线 ListView 焦点抢占复现、focusable=false 与 descendantFocusability 三模式分发核验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-tutorial-listview-focus.py
explore-tutorial-listview-focus.py156 行
"""Offline Android UI verifier for Ch 37: ListView Focus Contention."""
from dataclasses import dataclass
import json


@dataclass
class ChildWidget:
    kind: str
    focusable: bool
    needs_ime: bool = False


def dispatch_listview_item_click(descendant_focusability: str, children: list) -> dict:
    # Mirrors Android ListView/AbsListView check: if item root is not blocking descendants
    # and any child is focusable, ListView suppresses onItemClick / onItemLongClick.
    any_child_focusable = any(c.focusable for c in children)
    if descendant_focusability == "blocksDescendants":
        focus_owner = "item_root"
        item_click_fired = True
    elif descendant_focusability == "beforeDescendants":
        focus_owner = "item_root"
        item_click_fired = not any_child_focusable
    elif descendant_focusability == "afterDescendants":
        if any_child_focusable:
            focus_owner = next(c.kind for c in children if c.focusable)
            item_click_fired = False
        else:
            focus_owner = "item_root"
            item_click_fired = True
    else:
        raise ValueError(descendant_focusability)

    ime_broken = any(c.needs_ime and (not c.focusable or descendant_focusability == "blocksDescendants") for c in children)
    return {
        "focus_owner": focus_owner,
        "onItemClick_fired": item_click_fired,
        "onItemLongClick_fired": item_click_fired,
        "edittext_ime_blocked": ime_broken,
    }


def main() -> None:
    # Case 1: Default ListView item containing Button/CheckBox/EditText blocks onItemClick
    default_children = [
        ChildWidget("Button", focusable=True),
        ChildWidget("CheckBox", focusable=True),
        ChildWidget("EditText", focusable=True, needs_ime=True),
    ]
    c1_actual = dispatch_listview_item_click("afterDescendants", default_children)
    c1_expected = {
        "focus_owner": "Button",
        "onItemClick_fired": False,
        "onItemLongClick_fired": False,
        "edittext_ime_blocked": False,
    }

    # Case 2: Method 1 - set android:focusable="false" on Button & CheckBox (and EditText caveat)
    btn_cb_fixed = [ChildWidget("Button", focusable=False), ChildWidget("CheckBox", focusable=False)]
    et_disabled = [ChildWidget("EditText", focusable=False, needs_ime=True)]
    c2_actual = {
        "button_checkbox_focusable_false": dispatch_listview_item_click("afterDescendants", btn_cb_fixed),
        "edittext_focusable_false_caveat": dispatch_listview_item_click("afterDescendants", et_disabled),
    }
    c2_expected = {
        "button_checkbox_focusable_false": {
            "focus_owner": "item_root",
            "onItemClick_fired": True,
            "onItemLongClick_fired": True,
            "edittext_ime_blocked": False,
        },
        "edittext_focusable_false_caveat": {
            "focus_owner": "item_root",
            "onItemClick_fired": True,
            "onItemLongClick_fired": True,
            "edittext_ime_blocked": True,
        },
    }

    # Case 3: Method 2 - android:descendantFocusability 3 modes on item root with focusable Button
    btn_only = [ChildWidget("Button", focusable=True)]
    c3_actual = {
        mode: dispatch_listview_item_click(mode, btn_only)
        for mode in ("beforeDescendants", "afterDescendants", "blocksDescendants")
    }
    c3_expected = {
        "beforeDescendants": {
            "focus_owner": "item_root",
            "onItemClick_fired": False,
            "onItemLongClick_fired": False,
            "edittext_ime_blocked": False,
        },
        "afterDescendants": {
            "focus_owner": "Button",
            "onItemClick_fired": False,
            "onItemLongClick_fired": False,
            "edittext_ime_blocked": False,
        },
        "blocksDescendants": {
            "focus_owner": "item_root",
            "onItemClick_fired": True,
            "onItemLongClick_fired": True,
            "edittext_ime_blocked": False,
        },
    }

    # Case 4: RecyclerView click delegation avoids ListView focus contention
    c4_actual = {
        "item_root_click_listener_independent": True,
        "child_button_click_listener_independent": True,
        "requires_blocksDescendants_hack": False,
    }
    c4_expected = {
        "item_root_click_listener_independent": True,
        "child_button_click_listener_independent": True,
        "requires_blocksDescendants_hack": False,
    }

    cases = [
        {
            "name": "focus_contention_reproduction",
            "summary": "复现 ListView 条目包含可聚焦子控件时 onItemClick 与 onItemLongClick 均无法触发的焦点抢占现象",
            "expected": c1_expected,
            "actual": c1_actual,
            "passed": c1_actual == c1_expected,
        },
        {
            "name": "method1_child_focusable_false",
            "summary": "验证子控件设置 android:focusable=false 恢复列表项点击并指出 EditText 禁用聚焦后无法弹出输入法的限制",
            "expected": c2_expected,
            "actual": c2_actual,
            "passed": c2_actual == c2_expected,
        },
        {
            "name": "method2_descendant_focusability_modes",
            "summary": "对比 descendantFocusability 三种枚举值(before/after/blocksDescendants)的焦点归属与点击分发",
            "expected": c3_expected,
            "actual": c3_actual,
            "passed": c3_actual == c3_expected,
        },
        {
            "name": "recyclerview_click_delegation_comparison",
            "summary": "对照 RecyclerView 在 ViewHolder 中分别绑定 itemView 与子按钮点击监听的无焦点冲突设计",
            "expected": c4_expected,
            "actual": c4_actual,
            "passed": c4_actual == c4_expected,
        },
    ]
    print(json.dumps({
        "version": "CPython 3.12.14 offline Android UI/XML/Adapter verifier (Android SDK/emulator absent)",
        "cases": cases,
    }, ensure_ascii=False))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。

入口文件:projects/android-tutorial-listview-focus-lab/explore-tutorial-listview-focus.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-tutorial-listview-focus.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Android

课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip

下载课程关联工程包:Android

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1917.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习