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

运行离线 ScrollView 单子元素校验、异步偏移钳位、滚动条属性与 fling 速度折半核验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-tutorial-scrollview.py
explore-tutorial-scrollview.py147 行
"""Offline Android UI verifier for Ch 31: ScrollView."""
import json
import xml.etree.ElementTree as ET

ANDROID_NS = "{http://schemas.android.com/apk/res/android}"

VALID_SCROLL_XML = """<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView"
    android:scrollbarThumbVertical="@drawable/thumb_v"
    android:scrollbars="none">
    <LinearLayout android:orientation="vertical">
        <TextView android:id="@+id/txt_show" />
        <Button android:id="@+id/btn_down" />
    </LinearLayout>
</ScrollView>
"""

INVALID_SCROLL_XML = """<ScrollView xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView android:id="@+id/a" />
    <TextView android:id="@+id/b" />
</ScrollView>
"""


def validate_single_child(xml_str: str) -> dict:
    root = ET.fromstring(xml_str)
    direct_children = list(root)
    if len(direct_children) > 1:
        return {"valid": False, "direct_children": len(direct_children), "error": "IllegalStateException: ScrollView can host only one direct child"}
    return {"valid": True, "direct_children": len(direct_children), "error": None}


def scroll_to_bottom_offset(inner_measured_h: int, scroll_h: int) -> int:
    offset = inner_measured_h - scroll_h
    return 0 if offset < 0 else offset


def main() -> None:
    c1_actual = {
        "wrapped_linear_layout": validate_single_child(VALID_SCROLL_XML),
        "two_direct_textviews": validate_single_child(INVALID_SCROLL_XML),
    }
    c1_expected = {
        "wrapped_linear_layout": {"valid": True, "direct_children": 1, "error": None},
        "two_direct_textviews": {
            "valid": False,
            "direct_children": 2,
            "error": "IllegalStateException: ScrollView can host only one direct child",
        },
    }

    # Case 2: fullScroll(FOCUS_DOWN/FOCUS_UP) & async Handler.post scrollToBottom offset clamp
    lines_text = "".join(f"条目 * {i}\n" for i in range(1, 101))
    scroll_y = 0
    # FOCUS_DOWN on 2400px inner inside 800px viewport -> 1600
    scroll_y = scroll_to_bottom_offset(2400, 800)
    after_down = scroll_y
    # FOCUS_UP -> 0
    scroll_y = 0
    after_up = scroll_y
    # Short content (400px inside 800px viewport) -> clamps to 0
    short_offset = scroll_to_bottom_offset(400, 800)
    c2_actual = {
        "generated_lines": lines_text.count("\n"),
        "after_focus_down": after_down,
        "after_focus_up": after_up,
        "short_content_clamped": short_offset,
    }
    c2_expected = {
        "generated_lines": 100,
        "after_focus_down": 1600,
        "after_focus_up": 0,
        "short_content_clamped": 0,
    }

    # Case 3: Scrollbar attributes & programmatic visibility
    sv = ET.fromstring(VALID_SCROLL_XML)
    vertical_bar_enabled = sv.attrib.get(f"{ANDROID_NS}scrollbars") != "none"
    c3_actual = {
        "thumb_vertical": sv.attrib[f"{ANDROID_NS}scrollbarThumbVertical"],
        "scrollbars_attr": sv.attrib[f"{ANDROID_NS}scrollbars"],
        "vertical_bar_enabled": vertical_bar_enabled,
        "inherits_from": "FrameLayout",
    }
    c3_expected = {
        "thumb_vertical": "@drawable/thumb_v",
        "scrollbars_attr": "none",
        "vertical_bar_enabled": False,
        "inherits_from": "FrameLayout",
    }

    # Case 4: Custom ScrollView fling(velocityY / 2) velocity & deceleration distance
    v0 = 4000
    v_custom = v0 // 2
    decel = 2000.0
    dist_orig = (v0 * v0) / (2.0 * decel)
    dist_custom = (v_custom * v_custom) / (2.0 * decel)
    c4_actual = {
        "original_velocity": v0,
        "halved_velocity": v_custom,
        "distance_ratio": round(dist_custom / dist_orig, 4),
    }
    c4_expected = {
        "original_velocity": 4000,
        "halved_velocity": 2000,
        "distance_ratio": 0.25,
    }

    cases = [
        {
            "name": "single_child_invariant_check",
            "summary": "校验 ScrollView 单一直接子元素不变量(单个容器包装通过,多个直接子 View 触发 IllegalStateException)",
            "expected": c1_expected,
            "actual": c1_actual,
            "passed": c1_actual == c1_expected,
        },
        {
            "name": "fullscroll_and_async_offset_clamp",
            "summary": "模拟 fullScroll(FOCUS_DOWN/UP) 与 Handler.post 偏移计算(含短内容钳位到 0)",
            "expected": c2_expected,
            "actual": c2_actual,
            "passed": c2_actual == c2_expected,
        },
        {
            "name": "scrollbar_attributes_and_visibility",
            "summary": "解析滚动条滑块属性 scrollbarThumbVertical、scrollbars=none 与 FrameLayout 继承关系",
            "expected": c3_expected,
            "actual": c3_actual,
            "passed": c3_actual == c3_expected,
        },
        {
            "name": "custom_fling_velocity_scaling",
            "summary": "验证重写 fling(velocityY / 2) 将初速度减半后惯性滑行距离变为原距离 1/4",
            "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-scrollview-lab/explore-tutorial-scrollview.py

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

下载课程关联工程包:Android

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

下载课程关联工程包:Android

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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