(1) ScrollListener
1. RecyclerView 및 Adapter 설정
2. RecyclerView에 ScrollListener를 추가하여 스크롤 위치를 감지하고 새로운 데이터를 로드
// class SearchFragment
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
searchAdapter = SearchAdapter(object : SearchAdapter.OnItemClickListener {
override fun onClick(data: HomeVideoModel) {
(requireActivity() as MainActivity).openVideoDetailFromHome(data)
}
})
setRecyclerView()
updateToScroll()
observeViewModel()
}
private fun updateToScroll() {
binding.recyclerViewSearch.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
if (!binding.searchRecyclerContainer.canScrollVertically(1)) {
val listSize = viewModel.searchVideo.value!!.size - 1
viewModel.extraSearchVideos(binding.etSearch.text.toString())
(binding.recyclerViewSearch.adapter as SearchAdapter).notifyItemRangeChanged(
listSize,
8
)
}
}
})
}
3. 네트워크 요청을 통하여 새로운 데이터를 가져오기
private fun observeViewModel() {
viewModel.searchVideo.observe(viewLifecycleOwner) { searchVideo ->
if (searchVideo != null) {
searchAdapter.setItem(searchVideo)
(binding.recyclerViewSearch.adapter as SearchAdapter).notifyDataSetChanged()
}
}
}
------------------------------------------------------------------------------------------------
(2) Paging3
https://velog.io/@godmin66/Paging-3-%EC%A0%81%EC%9A%A9%ED%95%98%EA%B8%B0
728x90
'앱 개발 > Kotlin_Android' 카테고리의 다른 글
검색 결과 화면을 유지하기 (0) | 2024.06.25 |
---|---|
DTO, DTA, Entity (0) | 2024.06.21 |
Process, Thread (0) | 2024.06.13 |
MVVM 패턴 사용 이유 (0) | 2024.06.11 |
Android 4대 컴포넌트 (0) | 2024.06.07 |