본문 바로가기

Android + Kotlin

[Android + Kotlin] IllegalStateException: Page(s) contain a ViewGroup with a LayoutTransition (or animateLayoutChanges="true") 에러 수정

반응형

요즘 업무가 바쁘다보니, 게시글 작성이 뜸합니다.

방금 간단한 exception 을 잡게되어 짧게 글 작성합니다.

 

RecyclerView 의 adapter에서 item layout에

android:animateLayoutChanges="true"

를 적용하였습니다.

 

그런데, 아래와 같은 exception이 발생!!

 

java.lang.IllegalStateException: Page(s) contain a ViewGroup with a LayoutTransition (or animateLayoutChanges="true"), which interferes with the scrolling animation. Make sure to call getLayoutTransition().setAnimateParentHierarchy(false) on all ViewGroups with a LayoutTransition before an animation is started.

페이지에 LayoutTransitions가 포함된 경우 해당 LayoutTransitions에는 animateParentHierarchy가 false로 설정되어 있어야 합니다. 레이아웃 xml 파일에 animateLayoutChanges="true"인 ViewGroup이 있는 경우 LayoutTransition이 해당 ViewGroup에 자동으로 추가됩니다. xml 레이아웃을 확장한 후 해당 ViewGroup에서 getLayoutTransition().setAnimateParentHierarchy(false)를 수동으로 호출해야 합니다.

 

요런 내용인 것 같습니다.

 

내용처럼 setAnimateParentHierarchy(false) 를 해주면 된다고 합니다.

 

adapter의 item이기때문에 layout이 attached가 된 후에 호출해주어야 합니다.

inner class ViewHolder(
    private val binding: ItemBinding
) : RecyclerView.ViewHolder(binding.root) {

    init {
        with(binding) {
            itemView.doOnAttach {
                (it as? ViewGroup)?.layoutTransition?.setAnimateParentHierarchy(false)
            }
        }
    }
}

 

위의 코드와 같이 ViewHolder의 init() 에서 itemView의 doOnAttach가 호출된 후에 함수 호출하여 exception을 수정하였습니다.

 

끝!!!

반응형