Почему RecyclerView прокручивает и пропускает ячейки, в которых вложенные RecyclerView пусты? - PullRequest
0 голосов
/ 12 июля 2020

У меня есть родительский RecyclerView (RV)

user_content_recyclerView.apply {
    layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)

    contentAdapter = UserContentAdapter(
        contentSections,
        userInfo,
        itemViewModel,
        viewLifecycleOwner,
        context
    )
    adapter = contentAdapter

    isNestedScrollingEnabled = false
}

, который содержит разделы RelativeLayout, в каждом из которых есть TextView и RV.

<RelativeLayout ... >
    <RelativeLayout
        android:id="@+id/no_content_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:visibility="invisible">

            <TextView
                android:id="@+id/no_content_lbl"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginStart="16dp"
                android:textColor="@color/colorAppHideText"
                android:textSize="20sp" />
    </RelativeLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/section_recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>
recyclerView.apply {
    layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
    if (items != null) {
        val userContentItemAdapter = UserContentItemAdapter(
            items,
            itemViewModel,
            viewLifecycleOwner,
            this@UserContentAdapter.context
        )
        adapter = userContentItemAdapter
    }
    setRecycledViewPool(viewPool)
}

Если в разделе есть пустой RV, тогда я помещаю параметр видимости RV 'invisible' и TextView - 'visible' (например, есть строка вроде «Нет содержимого»).

if (items == null || items.isEmpty()) {
    recyclerView.visibility = View.INVISIBLE
    noContentLayout.visibility = View.VISIBLE
    noContent.text = "No content"
}

enter image description here

If the section is not empty then conversely.

If the first section is not empty then all is fine. RV is loading and nothing bad happens. But if the first N sections are empty, the RV automatically scrolls these N until the first is not empty. And it is a problem because I don't want such behavior.

I tried to use

  1. Scrolling to top inside my parent RV. For llmanager linearLayoutManager.scrollToPositionWithOffset(0, 0) or for RV .scrollTo(0, 0), .scrollToPosition(0), .smoothScrollToPosition(0)

Didn't help.

  1. CoroutineScope.
layoutManager = object : LinearLayoutManager(context, RecyclerView.VERTICAL, false) {
    override fun canScrollVertically(): Boolean {
        return false
    }
}

viewLifecycleOwner.lifecycleScope.launch(Dispatchers.Main) {
    delay(1000)
    val manager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
    layoutManager = manager
    manager.scrollToPositionWithOffset(0, 0)
}

Первые 1 секунду все в порядке, но после прокрутки вниз снова. Не помогло.

Добавление фальшивого предмета во вложенный RV в первом разделе. Это работает, но я думаю, что это не очень хорошее решение.

Есть другие идеи?

EDIT

Я создал небольшой образец с этой ошибкой вы можете увидеть здесь

введите описание изображения здесь

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...