Antilog의 개발로 쓰다
article thumbnail
반응형


공식문서

 

RecyclerView.ItemDecoration  |  Android 개발자  |  Android Developers

ItemDecoration abstract class ItemDecoration Known Direct Subclasses DividerItemDecoration DividerItemDecoration is a RecyclerView.ItemDecoration that can be used as a divider between items of a LinearLayoutManager. ItemTouchHelper This is a utility class

developer.android.com

ItemDecoration 이란?

해당 추상 클래스를 이용하여 항목, 하이라이트, 시작적 그룹화 경계 사이에 구분선을 그리는데 유용하게 사용할 수 있습니다. 

ItemDecoration 을 사용하는 이유

다음과 같이 ListView의 경우 자체적으로 구분선을 그리는 기능이 존재합니다.

하지만 RecyclerView의 경우 ListView와 같이 사용하는 것이 불가능하고 구분선을 그릴 수 있는 ItemDecoration을 사용해야한다.

그러나 대부분 아래와 같은 방법으로 구분선을 그리곤 하는데

위와 같은 방법을 사용하면 아래와 같은 문제가 발생한다고 합니다.

  • 퍼포먼스에 영향을 준다.
  • 좌우 슬라이드시 하단 구분선이 같이 움직인다.
  • 각각 구분선을 통제할 수 없어진다.

출처 - https://readyandroid.wordpress.com/recyclerview-itemdecoration-in-android/

 

RecyclerView ItemDecoration in Android

Part 1 : Avoid adding dividers to the view layout ItemDecoration can be drawn to all four sides of RecyclerView items First things first. So what is ItemDecoration? This is how official doc puts it…

readyandroid.wordpress.com

자세한 내용은 위 링크에서 확인할 수 있습니다.

ItemDecoration 간단 구현(그냥 구분선 추가하기)

*Adapter를 지정하기 이전에 ItemDecoration을 추가

VERTICAL의 경우 지정한 LayoutManager에 따라 import해주시면 됩니다.

ItemDecoration 간단 커스텀

이런저런 다양한 커스텀 방법이 많아 커스텀을 원하는 경우 더 많은 자료를 찾는 것을 권장합니다.

rv_main.addItemDecoration(MyDecoration(100, Color.BLACK))

위와 같이 적용하면 아래와 같은 화면이 됩니다.

코드 참고-> https://heepie.me/367

onDraw와 onDrawOver의 차이

onDraw

리사이클러뷰가 그려지기 전에 먼저 그려진다. 따라서 아이템들보다 아래 그려진다.

onDrawOver

리사이클러뷰가 그려진 후 그려진다. 따라서 아이템들보다 위에 그려진다.

다른 정보를 확인하시려면 공식 문서를 확인해주세요

반응형
profile

Antilog의 개발로 쓰다

@Parker_J_S

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

profile on loading

Loading...