如何为RecyclerView添加可访问性?

3

我需要为RecyclerView添加辅助功能。我的RecyclerView中有7个项目,我希望TalkBack可以一次性读取它们所有。

这是我的RecyclerView小部件,请给予建议。属性 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" />

回收站适配器视图如下:

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
    }
}

我猜你需要为RecyclerView的子项添加可访问性,即为RecyclerView的适配器添加可访问性。 - bhuvnesh pattnaik
你在子XML中吗? - Allen rosh
2个回答

5

我认为你应该在RecycleView以及它的子项中添加importantForAccessibility属性:

父级布局:

 <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中添加了这个方法,以便将聚焦的项目保持在顶部。

在您的activity/fragment中:

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


0

你应该像之前一样为recycler设置适配器。 在recycler的适配器中,通过将其设置为public或者使用getter方法,你可以访问到你的列表。


我正在设置适配器如上所述。由于我需要一次读取所有列表项,因为我在适配器中传递了字符串列表,所以我需要做哪些更改? - Allen rosh
我尝试在适配器中添加内容描述,但它并没有达到我的目的。holder.txtExitRowItem?.contentDescription = items.get(position) ...... 我的目标是一次性读取完整的Recycler View,也就是7行。 - Allen rosh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接