본문 바로가기

Android

[Android] Parent View가 clickable 일 때 SeekBar 버그(?)

SeekBar가 아닌 뒷 배경을 눌렀을 때 Seekbar에 Ripple Effect가 발생하는 모습

 뒤에 위치한 View에 Touch Event가 가는 것을 막기 위해 SeekBar의 Parent View에 android:clickable="true"를 주었습니다. Touch Event가 가는 건 막았지만 Parent View를 터치했을 때 SeekBar에 변화가 생기는 문제가 있었습니다.

 SeekBar에도 android:clickable="true"를 주어 현상을 해결할 수 있었습니다.

SeekBar에 변화가 생기지 않는 모습

문제가 생기는 Layout xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <SeekBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="24dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

해결한 Layout xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:focusable="true"
    tools:context=".MainActivity">

    <SeekBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="24dp"
        android:clickable="true"
        android:focusable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

android:focusable="true"을 같이 주는 이유는 Android에서 권장하기 때문입니다.

focusable도 추가해 달라는 ToolTip

저런 현상이 발생하는 이유는 잘 모르겠지만, 속성을 추가해서 간단히 고칠 수 있어 다행인 거 같습니다.

 

ps) https://stackoverflow.com/questions/39622270/seekbar-focus-not-wanted?rq=1

 

Seekbar focus not wanted

I am using a SeekBar in my layout and I have another View that is a RelativeLayout. ... <seekbar< p=""> </seekbar<>

stackoverflow.com

"stack over flow" 의 답변을 보면 ViewGroup안 dispatchSetPressed에서의 처리가 해당 현상을 발생 시킨다고 나와있습니다.

dispatchSetPressed를 보면 pressed가 true일 때 자식 View들을 돌면서 clickable, longClickable 둘 다 false인 자식들에게 setPressed(true)를 합니다.