android - 如何为 RecyclerView 添加可访问性?
问题描述
我需要为 RecyclerView 添加可访问性。我在 RecyclerView 中有 7 个项目,我希望对讲一次读取它们。
这是我的回收站视图小部件。请指教。属性importantforaccessibility= "yes"
没有帮助。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginTop="24dp"
android:layout_marginRight="16dp"
android:orientation="vertical"
android:paddingBottom="16dp">
<TextView
android:id="@+id/txt_section"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
android:orientation="vertical"
android:importantForAccessibility="yes"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
Recycler 适配器视图如下:
class ExitRowAgreementAdapter(val items: List<String>, val context: Context) :
RecyclerView.Adapter<ExitRowAgreementAdapter.ExitRowViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ExitRowViewHolder {
return ExitRowViewHolder(
LayoutInflater.from(context).inflate(
R.layout.row_items,
parent,
false
)
)
}
override fun getItemCount(): Int {
return items.size
}
override fun onBindViewHolder(holder: ExitRowViewHolder, position: Int) {
holder.txtExitRowItem.text = items.get(position)
}
inner class ExitRowViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val txtExitRowItem = view.txt_exit_row_item
}
}
解决方案
我认为您应该将 importantForAccessibility 添加到 RecycleView 以及他的孩子:
父布局:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/provider_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
*** android:importantForAccessibility="yes" ***
tools:listitem="@layout/list_item" />
在项目视图中:
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root"
style="@style/CardViewStyle"
**** android:focusable="true" ****
**** android:importantForAccessibility="yes" ****>
<TextView
android:id="@+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</androidx.cardview.widget.CardView>
此外,我在列表导航方面遇到了问题,因此我将此方法添加到 recyclerView 以将焦点项目保持在顶部。
在您的活动/片段中:
recyclerView.setAccessibilityDelegate(new View.AccessibilityDelegate() {
@Override
public boolean onRequestSendAccessibilityEvent(ViewGroup host, View child, AccessibilityEvent event) {
int position;
switch (event.getEventType()) {
case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED:
position = (Integer) child.getTag();
((LinearLayoutManager) recyclerView.getLayoutManager()).scrollToPositionWithOffset(position, 100);
break;
}
return super.onRequestSendAccessibilityEvent(host, child, event);
}
});
在 recyclerView 适配器中:
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
.....
holder.itemView.setTag(position);
}
如果您想更深入,这篇文章对我有很大帮助:https ://android.jlelse.eu/accessibility-and-staggered-grid-layout-8e01a753aada
推荐阅读
- zap - 将后端端口添加到 ZAP 范围
- spring-boot - 当表有大量记录时休眠无法更改表
- leaflet - 设置工具提示不透明度
- amazon-web-services - AWS SAM 指定 zip 文件名并使用版本控制
- java - 如何更改 Java 注释的默认字段名称
- express - Express + Nunjucks - 有没有类似于 Django 的 {% url "..." %} 的模板标签
- applescript - Applescript 将选定的单词和定义复制到 Notes
- android - 如何在 Jitpack 上使用 maven 为 Kotlin 库发布 KDoc?
- python - 如何从 django-import-export 获取文件?
- wordpress - 在 Wordpress 中查询 ACF 关系字段