참고자료 : WindowManager | Android Developers
WindowManager | Android Developers
android.inputmethodservice
developer.android.com
우선 구현이 완료된 영상을 확인해보자 ~
구현하고자 했던 뷰는 다음과 같았다.
“할일을 추가해 보세요” Empty 뷰가, 스크롤되는 제목 부분을 제외하고 가운데 높이에 정렬되도록 만들어야 했다.
그러나
그래서 다음과 같은 방법으로 해결하고자 했다.
fun Activity.getWindowHeight(): Int {
val windowManager = this.getSystemService(Context.WINDOW_SERVICE) as WindowManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val windowMetrics = windowManager.currentWindowMetrics
val insets = windowMetrics.windowInsets
.getInsetsIgnoringVisibility(WindowInsets.Type.systemBars())
return windowMetrics.bounds.height() - insets.bottom - insets.top
} else {
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
return displayMetrics.heightPixels
}
}
binding.appbarMyTodo.viewTreeObserver.addOnGlobalLayoutListener(object :
ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
binding.appbarOurTodo.viewTreeObserver.removeOnGlobalLayoutListener(this)
val displayHeight = activity?.getWindowHeight() ?: return
val toolbarHeight = binding.toolbarMyTodo.height
val appBarHeight = binding.appbarMyTodo.totalScrollRange
binding.layoutOurTodoEmpty.layoutParams = (binding.layoutOurTodoEmpty.layoutParams).also {
it.height = displayHeight - toolbarHeight - appBarHeight
}
}
})
binding.appbarOurTodo.addOnOffsetChangedListener { appBarLayout, verticalOffset ->
val displayHeight = activity?.getWindowHeight() ?: return@addOnOffsetChangedListener
val toolbarHeight = binding.toolbarOurTodo.height
val appBarHeight = appBarLayout.totalScrollRange + verticalOffset
binding.layoutOurTodoEmpty.layoutParams = (binding.layoutOurTodoEmpty.layoutParams).also {
it.height = displayHeight - toolbarHeight - appBarHeight
}
}
아자아자~~
KakaoLink 카카오 공유 한 번 해볼래? (0) | 2024.01.19 |
---|---|
ProgressBar 커스텀 적용기 (0) | 2024.01.18 |
카카오 웹 로그인은 안하는걸로 하지 않을래...? (2) | 2024.01.13 |
이모지를 포함한 글자수를 세는 방법 🤬 (1) | 2024.01.02 |
안드로이드 MVVM + 클린아키텍처는 어떻게 쓰는걸까? (2) | 2023.12.28 |