ANDROID

RecyclerView 스크롤다운시 아이템 간격 벌어짐 현상

장꾸꾸 2021. 4. 13. 23:38

RecyclerView에서 스크롤을 내릴 때 기대하지 않은 큰 간격이 발생할 수 있습니다. ~~ListView에서는 없던 일인데~~

스크롤 내리기 전에는 멀쩡했는데 내렸다 올려보니까 아이템들끼리 너무 내외하는 상황에 당황했어요. 🙁

 

역시 답은 StackOverflow에 있었습니다.

stackoverflow.com/questions/35817610/large-gap-forms-between-recyclerview-items-when-scrolling-down

 

Large gap forms between RecyclerView items when scrolling down

I'm making a ToDo list app, and while testing it, for some reason, a huge gap forms between the items whenever I try to scroll down. It always happens whenever I Drag and Drop the items, but I don'...

stackoverflow.com

img

With the release 2.3.0 there is an exciting new feature to the LayoutManager API: auto-measurement! This allows a RecyclerView to size itself based on the size of its contents. >This means that previously unavailable scenarios, such as using WRAP_CONTENT >for a dimension of the RecyclerView, are now possible. You’ll find all built in LayoutManagers now support auto-measurement.

 

요약해보자면 2.30 release부터 auto-measurement가 적용되기 시작합니다. RecyclerView 자체적으로 간격을 조정할 수 있게 된다는 건데요. 기존에 무시됐던 아이템 레이아웃 내의 match_parent가 그대로 반영됩니다.

따라서 의도하지 않았던 경우라면 RecyclerView에 들어갈 item들의 레이아웃 height를 wrap_content로 변경하면 스크롤 다운시의 large gap 현상을 없앨 수 있습니다. parent layout도 동일하게 wrap_content로 설정해주어야 합니다.