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
- 안드로이드
- 백준
- CustomView
- Android
- DataBinding
- LiveData
- onMeasure
- View
- HTTP
- Algorithm
- recyclerview
- 코틀린
- room
- CollapsingToolbarLayout
- BOJ
- lifecycle
- sqlite
- notification
- 알림
- activity
- Coroutine
- onLayout
- Behavior
- CoordinatorLayout
- hilt
- AppBarLayout
- kotlin
- Navigation
- 알고리즘
- ViewModel
Archives
- Today
- Total
개발일지
Android in A..Z - RecyclerView (Selection - Observer) 본문
Android (안드로이드)/RecyclerView
Android in A..Z - RecyclerView (Selection - Observer)
강태종 2021. 1. 15. 04:32SelectionObserver
Selection에 Observer을 통해 콜백 함수를 작성할 수 있다.
Selection이 되면 Delete Option Menu가 생기는 예제이다.
addObserver(object : SelectionTracker.SelectionObserver<Long>() {
override fun onSelectionChanged() {
super.onSelectionChanged()
val tracker = this@apply
if (tracker.hasSelection() && menu.findItem(MENU_DELETE) == null) {
menu.add(Menu.NONE, MENU_DELETE, Menu.NONE, "Delete")
.setIcon(R.drawable.ic_delete)
.setOnMenuItemClickListener {
tracker.selection.forEach {
val holder = recyclerView.findViewHolderForItemId(it)
if (holder is SelectionAdapter.SelectionHolder) {
list.remove(holder.element)
}
}
tracker.clearSelection()
adapter.notifyDataSetChanged()
true
}
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)
} else if (!tracker.hasSelection() && menu.findItem(MENU_DELETE) != null){
menu.removeItem(MENU_DELETE)
}
}
})
- onSelectionChanged : Selection에 변화가 있을 때 호출된다.
Git (예제코드)
github.com/KangTaeJong98/Example/tree/main/Android/RecyclerView
'Android (안드로이드) > RecyclerView' 카테고리의 다른 글
Android in A..Z - ItemTouchHelper disable for some holder (0) | 2021.08.02 |
---|---|
Android in A..Z - RecyclerView (SnapHelper) (0) | 2021.01.21 |
Android in A..Z - RecyclerView (Selection - Predicate) (0) | 2021.01.15 |
Android in A..Z - RecyclerView (Selection Tracker) (0) | 2021.01.15 |
Android in A..Z - RecyclerView (payload) (0) | 2021.01.10 |
Comments