RecyclerView의 LayoutManager(Linear, Grid)

지프치프·2022년 6월 24일
0

Android

목록 보기
52/91
post-thumbnail

“Android 로봇은 Google에서 제작하여 공유한 저작물을 복제하거나 수정한 것으로 Creative Commons 3.0 저작자 표시 라이선스의 약관에 따라 사용되었습니다.”

개요

RecyclerView의 item들을 배치하는 방법은 Linear, Grid 2가지가 있다.

  • Linear
    item들을 선형으로 배치한다.
  • Grid
    item들을 격자형으로 배치한다.

각 Layout들은 HORIZONTAL(수평), VERTICAL(수평)를 모두 지원한다.

Linear

생성자는 context를 받고
setOrientation() 메소드를 통해 수직과 수평을 set 해준다.
orientation을 생략하면 기본적으로 Vertical(수직)이다.

HORIZONTAL(수평)

binding.rvRecycle.layoutManager = LinearLayoutManager(this).apply { 
	orientation = LinearLayoutManager.HORIZONTAL
 }

VERTICAL(수직)

binding.rvRecycle.layoutManager = LinearLayoutManager(this).apply { 
	orientation = LinearLayoutManager.VERTICAL
}

Grid

생성자로 context와 몇 줄로 배치할 지를 입력한다.(spanNum)
추가적으로 orientation을 설정해줄 수 있으며
마찬가지로 생략할 경우엔 기본적으로 Vertical(수직)이다.

HORIZONTAL(수평)

binding.rvRecycle.layoutManager = GridLayoutManager(this, 2, GridLayoutManager.HORIZONTAL, false)

VERTICAL(수직)

binding.rvRecycle.layoutManager = GridLayoutManager(this, 2)

개인적으로 공부했던 것을 바탕으로 작성하다보니
잘못된 정보가 있을수도 있습니다.
인지하게 되면 추후 수정하겠습니다.
피드백은 언제나 환영합니다.
읽어주셔서 감사합니다.

profile
지프처럼 거침없는 개발을 하고싶은 개발자

0개의 댓글

Powered by GraphCDN, the GraphQL CDN