Android
Android ScrollView 보이는 영역 체크
Wook No.1
2024. 9. 19. 14:05
RecyclerView는 LayoutMager에서 제공하는 함수를 통해 현재 보이는 영역을 체크할수 있다.
(findFirstVisibleItemPosition, findLastVisibleItemPosition)
ScrollView는 체크하고 싶은 View가 보이는지 확인 하려면 getLocalVisibleRect 함수를 통해 노출여부를 확인 하면 된다.
val scrollBounds = Rect()
scrollMain.getHitRect(scrollBounds)
scrollMain.setOnScrollChangeListener{ v, scrollX, scrollY, oldScrollX, oldScrollY ->
if(checkView.getLocalVisibleRect(scrollBounds)) {
// checkView Visible
}
}
커스텀 checkView를 만들어서 안에 있는 Child View가 보이는지 체크하기 위해서는 rect의 top, bottom 사이에 있는지 확인 하는 방법으로 처리 할수 있다.
val scrollBounds = Rect()
scrollMain.getHitRect(scrollBounds)
scrollMain.setOnScrollChangeListener{ v, scrollX, scrollY, oldScrollX, oldScrollY ->
if(checkView.getLocalVisibleRect(scrollBounds)) {
if(visibleCheckChildView(checkView, scrollBounds) {
// check visible childView
}
}
}
fun visibleCheckChildView(checkView: View, scrollBounds: Rect): Boolean {
val checkChildView = checkView.findViewById(View)(R.id.child_view) // childview id
return (checkView.top in scrollBounds.top..scrollBounds.bottom)
|| (checkView.bottom in scrollBounds.top..scrollBounds.bottom)
}