Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- room
- View
- notification
- CoordinatorLayout
- hilt
- Coroutine
- sqlite
- HTTP
- 알고리즘
- Navigation
- 코틀린
- Android
- onMeasure
- onLayout
- 백준
- 안드로이드
- 알림
- CollapsingToolbarLayout
- activity
- AppBarLayout
- recyclerview
- kotlin
- lifecycle
- BOJ
- Behavior
- Algorithm
- DataBinding
- ViewModel
- LiveData
- CustomView
Archives
- Today
- Total
개발일지
Android in A..Z - RecyclerView (SnapHelper) 본문
SnapHelper
RecyclerView를 ViewPager처럼 사용하거나 NumberPicker처럼 Scroll하다가 하나의 Item을 자석처럼 끌어당겨 하나의 Item을 선택하게 도와주는 클래스이다.
Kotlin
val snapHelper = LinearSnapHelper()
snapHelper.attachToRecyclerView(recyclerView)
OnScrollListener를 사용해서 SnapHelper가 선택한 position 받기
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
var currentPosition = RecyclerView.NO_POSITION
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if (recyclerView.layoutManager != null) {
val view = snapHelper.findSnapView(recyclerView.layoutManager)!!
val position = recyclerView.layoutManager!!.getPosition(view)
if (currentPosition != position) {
currentPosition = position
binding.position = currentPosition
}
}
}
})
Git (예제코드)
github.com/KangTaeJong98/Example/tree/main/Android/RecyclerView
'Android (안드로이드) > RecyclerView' 카테고리의 다른 글
Android in A..Z - RecyclerView (Selection - ItemKeyProvider) (0) | 2021.09.18 |
---|---|
Android in A..Z - ItemTouchHelper disable for some holder (0) | 2021.08.02 |
Android in A..Z - RecyclerView (Selection - Observer) (0) | 2021.01.15 |
Android in A..Z - RecyclerView (Selection - Predicate) (0) | 2021.01.15 |
Android in A..Z - RecyclerView (Selection Tracker) (0) | 2021.01.15 |
Comments