Анимация внутри LinearLayout когда внутренний элемент меняет свой размер

Не могу выложить всех деталей коммерческий проект, но..

  <org.mycom.ui.view.ExpandedLayout
        android:id="@+id/expandedLayout"
        android:layout_width="match_parent"
        android:visibility="gone"
        android:animateLayoutChanges="true"
        android:layout_height="wrap_content">

        <include
            android:id="@+id/layItemLoaded"
            layout="@layout/item_loaded_photo" />

    </org.mycom.ui.view.ExpandedLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rvCoincident"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:visibility="gone"
        android:background="@android:color/transparent"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        android:orientation="vertical"
        />

В ExpandedLayout лежит кнопка который сворачивает все элементы внутри в данный момент это элемент layItemLoaded, но RecyclerView не подтягивается когда пропадают вложенные элементы и остается пустое место, была предпринято две попытки, одна красива сворачивает, а другая подтягивает с морганием и не полностью погружная вложенные View

Первая попытка

private fun slideContent(){
    repeat(childCount - 1){ index ->
        val view = getChildAt(childCount - 1 - index)

        if (isExpanded) view.isVisible = true
        view.animate().apply {
            duration = 200
            scaleY(if (isExpanded) 1f else 0f)
            y(if (isExpanded) 100f else -100f)
            withEndAction {
                if (!isExpanded){
                   // view.isGone = true
                }
            }
        }
    }
}

Вторая попытка

private fun slideContent(){
    val isAdd = listHeight.isEmpty()
    if (_currentHeightParent == null) _currentHeightParent = height

    repeat(childCount - 1){ index ->
        val view = getChildAt(childCount - 1 - index)
        if (isAdd) listHeight.add(view.height)

        if (isExpanded) view.isVisible = true

        val currentHeight = listHeight[index]
        val currentParams = layoutParams

        view.animate().apply {
            setUpdateListener {
                val fraction = if (!isExpanded) 1f - it.animatedFraction else it.animatedFraction
                currentParams.height = (currentHeightParent - currentHeight) + (currentHeight * fraction).toInt()
                layoutParams = currentParams
                requestLayout()
            }
            duration = 200
            alpha(if (isExpanded) 1f else 0f)
            withEndAction {
                if (!isExpanded){
                   // view.isGone = true
                }
            }
        }
    }
}

Ответы (1 шт):

Автор решения: Svetl9chok

Может кому пригодится что бы при смене размера элементы не выглядели так как будто они на половину загружены, нужно производить расчет всего что внутри

view.animate().apply {
                    setUpdateListener {
                        val fraction = if (!isExpanded) 1f - it.animatedFraction else it.animatedFraction
                        val currentSize = (currentHeight * fraction).roundToInt()
    
                        view.layoutParams = currentParams
                        currentParams.height = if (isExpanded) {
                            if (currentHeight - currentSize < 10) currentHeight else currentSize
                        } else {
                            if (currentSize < 10) 0 else currentSize
                        }
    
                        if (view is ViewGroup){
                            view.children.iterator().forEach { outView ->
                                outView.requestLayout()
                            }
                        } else {
                            view.requestLayout()
                        }
                    }
                    duration = 200
                    alpha(if (isExpanded) 1f else 0f)
                    withEndAction {
                        if (!isExpanded){
                            view.isGone = true
                        }
                    }
                }
→ Ссылка