RTL Horizo​​ntal RecyclerView 在 CoordinatorLayout 中的 NestedScrollView 内无法正常工作

RTL Horizontal RecyclerView doesn't work properly inside a NestedScrollView in a CoordinatorLayout

我有一个阿拉伯语美食应用程序,我的应用程序是这样的:

如您所见,水平 RecyclerViews 是预期的 RTL。
在这个应用程序中,我有一个 Category 和一个 Recipe,类别在父 RecyclerView 中垂直填充,食谱在每个类别的子 RecyclerView 中水平填充. 这是主要布局的代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".ui.main.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvCategories"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/adView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/frameRecentPlaceholder" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        app:adSize="BANNER"
        app:adUnitId="@string/ADMOB_BANNER_ID"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

对于 Category 项:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvCategoryName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="8dp"
        android:textColor="@color/Black"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="@+id/btnViewAll"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/btnViewAll"
        tools:text="@string/milk" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvRecipes"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnViewAll" />

    <Button
        android:id="@+id/btnViewAll"
        android:layout_width="wrap_content"
        android:textStyle="bold"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/view_all"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

以及 Recipe 项。

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="300dp"
    android:layout_height="220dp"
    android:layout_margin="8dp"
    android:orientation="vertical"
    app:cardCornerRadius="16dp"
    app:cardElevation="1dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.65" />

        <ImageView
            android:id="@+id/ivRecipeImage"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            app:layout_constraintBottom_toTopOf="@+id/guideline2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0"
            tools:ignore="ContentDescription" />

        <TextView
            android:id="@+id/tvRecipeTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:textColor="@color/Black"
            android:textStyle="bold"
            app:layout_constraintBottom_toTopOf="@+id/tvRecipeCategories"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/ibOptions"
            app:layout_constraintTop_toTopOf="@+id/guideline2"
            app:layout_constraintVertical_chainStyle="packed"
            tools:text="البطاطس بالجبن الرومي" />

        <com.andalus.abomed7at55.mn_edek_a7la.custom.FontAwesomeTextView
            android:id="@+id/tvTagsIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:text="@string/tags_icon"
            android:textSize="12sp"
            android:textStyle="normal"
            app:layout_constraintBottom_toBottomOf="@+id/tvRecipeCategories"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/tvRecipeCategories" />

        <TextView
            android:id="@+id/tvRecipeCategories"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginEnd="2dp"
            android:layout_marginRight="2dp"
            android:layout_marginBottom="8dp"
            android:gravity="start"
            android:textSize="12sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/tvTagsIcon"
            app:layout_constraintStart_toEndOf="@+id/ibOptions"
            app:layout_constraintTop_toBottomOf="@+id/tvRecipeTitle"
            tools:text="حلويات" />

        <ImageButton
            android:id="@+id/ibOptions"
            android:layout_width="wrap_content"
            android:layout_height="24dp"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:adjustViewBounds="true"
            android:background="@android:color/transparent"
            android:scaleType="fitXY"
            android:src="@drawable/ic_more_vert_black_24dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/guideline2"
            tools:ignore="ContentDescription" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</com.google.android.material.card.MaterialCardView>

到目前为止一切顺利。当我尝试在我的父级(类别)RecyclerView 之上添加一个新片段并使其可滚动时,问题就开始了。
简而言之,我正在努力达到这种设计:

顶部部分不是recycler视图的项目,它来自一个片段,这是这个新设计的代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".ui.main.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <FrameLayout
        android:id="@+id/frameRecentPlaceholder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvCategories"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/adView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/frameRecentPlaceholder" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        app:adSize="BANNER"
        app:adUnitId="@string/ADMOB_BANNER_ID"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

到目前为止一切顺利,顶部不会随内容滚动,这是预期的行为,但是当我用 NestedScrollView 包裹布局时,应用程序开始表现异常,水平 RecyclerViwes开始垂直滚动,应用看起来很丑,代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_marginTop="?actionBarSize"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context=".ui.main.MainActivity"
        tools:showIn="@layout/app_bar_main">

        <FrameLayout
            android:id="@+id/frameRecentPlaceholder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvCategories"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toTopOf="@+id/adView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/frameRecentPlaceholder" />

        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            app:adSize="BANNER"
            app:adUnitId="@string/ADMOB_BANNER_ID"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />


    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

结果:

我认为这与 RTLreverseLayout 有关。
这是我的适配器的代码:

class CategoriesAdapter(
        private val onCategoryClicked: (category: String) -> Unit = {},
        var data: List<Category> = listOf(),
        private val onRecipeClicked: (id: Int) -> Unit = {},
        private val onOptionsClicked: (id: Int, optionsButton: View) -> Unit
) : RecyclerView.Adapter<CategoriesAdapter.CategoryHolder>(), Serializable {

    private lateinit var context: Context

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryHolder {
        context = parent.context
        val view = LayoutInflater.from(context).inflate(R.layout.item_category, parent, false)
        return CategoryHolder(view)
    }

    override fun onBindViewHolder(holder: CategoryHolder, position: Int) {
        holder.tvCategoryName.text = data[position].title

        data[position].recipesAdapter.onClick = {
            onRecipeClicked.invoke(it)
        }

        data[position].recipesAdapter.onOptionsClicked = { id, optionsButton ->
            onOptionsClicked.invoke(id, optionsButton)
        }

        holder.rvRecipes.adapter = data[position].recipesAdapter

    }

    override fun getItemCount(): Int {
        return data.size
    }

    inner class CategoryHolder(view: View) : RecyclerView.ViewHolder(view) {
        val tvCategoryName: TextView = itemView.tvCategoryName
        val rvRecipes: RecyclerView = itemView.rvRecipes

        //The most important part
        init {
            rvRecipes.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, true)
            rvRecipes.onFlingListener = null
            val snapHelper = LinearSnapHelper()
            snapHelper.attachToRecyclerView(rvRecipes)
            itemView.btnViewAll.setOnClickListener {
                onCategoryClicked.invoke(data[adapterPosition].tag)
            }
        }
    }
}

我希望有人能帮助我。

不使用 NestedScrollView,更简单的解决方案是使用不同的 view types