Android 如何在阅读列表项计数时停止包括页脚行的 TalkBack

标签 android listview footer talkback

我有一个包含笔记列表的对话框,我想让用户滚动到列表的末尾以找到“确定”按钮以关闭对话框。

对话框包含一个 ListView,我使用页脚行将按钮添加到列表中。

View footerView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).
                inflate(R.layout.list_footer_button, null, false);
listView.addFooterView(footerView);

当我打开回话并打开此对话框时,它会读取错误的 ListView 计数,因为它包括页脚行。例如,如果我的列表有 3 个项目,对讲显示为“第 1 个,共 4 个”。

问题

如何让 talkBack 读取正确数量的项目而忽略页脚行?

如果我不能改变它,我还可以如何创建一个对话框,用户必须滚动到列表的末尾才能看到关闭对话框的按钮。

最佳答案

我已通过将按钮添加到最后一行的 View 而不是添加页脚来解决此问题。

我的每行布局包含一个 textView 和一个按钮,并且按钮设置为“不可见”。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout android:id="@+id/notes_details"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <!-- other views here but removed for this post -->
        <TextView
            android:id="@+id/notes_text"
            style="@style/notes_text_style"/>
    </LinearLayout>

    <Button
        android:id="@+id/notes_button"
        style="@style/notes_button"
        android:layout_below="@id/notes_details"
        android:layout_centerHorizontal="true"
        android:visibility="invisible" />

</RelativeLayout>

在列表的适配器中,我根据该行是否是最后一行来显示/隐藏按钮

 boolean isLastRow = iPosition == getCount()-1;
 okButton.setVisibility(isLastRow ? View.VISIBLE : View.GONE);

需要更改辅助功能

还有一些额外的工作可以使这一切都用于可访问性目的。如果没有更改以支持可访问性,则只有行是可导航的。因此,最后一行将选择注释文本和确定按钮作为一个可选项目,您将无法使用键盘/跟踪球导航到该按钮。

首先你必须设置列表,使一行中的项目是可聚焦的

listView.setItemsCanFocus(true);

其次,在适配器的代码中,我相应地在注释 textView 和按钮上设置了 focusable/nextFocusXXX

// when in accessibility mode, we want to set the navigation order for
// the list item's text and the OK button
boolean isLastRow = iPosition == getCount()-1;
if (isAccessibilityEnabled) {
    notesTextView.setFocusable(true);
    okButton.setFocusable(true);
    rowView.setNextFocusForwardId(isLastRow ? R.id.notes_button: R.id.notes_text);
    rowView.setNextFocusDownId(isLastRow ? R.id.notes_button: R.id.notes_text);
    okButton.setNextFocusUpId(R.id.notes_text);
}

关于Android 如何在阅读列表项计数时停止包括页脚行的 TalkBack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43352034/

相关文章:

android - java.lang.IllegalStateException android.widget.ListView.layoutChildren 崩溃

listview - Xamarin.Forms - ListView 未在 OnAppearing 方法中更新

android - 如何在一个ListView中拥有多个数据适配器

Android Compose 显示和隐藏键盘

android - 你如何循环线程?

asp.net - 没有数据绑定(bind)时使 GridView 页脚可见

Android:如何向全屏 ScrollView 添加页脚?

Android API 21 - TimePicker 小部件

javascript - 有条件地更改每个列表项的背景颜色 JQuery Mobile

html - 以响应式方式创建粘性页脚