如何为RecyclerView添加辅助功能?

时间:2019-11-13 05:32:27

标签: android 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" />

回收站适配器视图如下:

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

2 个答案:

答案 0 :(得分:1)

我认为您应该向RecycleView及其子代添加重要的ForAccessibility

父级布局:

 <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

答案 1 :(得分:0)

完成后,您应该为回收站设置适配器。 在回收站适配器中,可以通过将其公开或设置getter来获得列表,您可以访问列表。