Android

NestedScrollView에서 버튼을 두번 클릭해야 작동하는 경우 해결방법

Bateaux 2018. 4. 14. 22:16
<android.support.design.widget.CoordinatorLayout>
    <android.support.design.widget.AppBarLayout>
        <android.support.design.widget.CollapsingToolbarLayout>
            <android.support.v7.widget.Toolbar>
                ...
            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <android.support.v4.widget.NestedScrollView>
            <LinearLayout>

                <RelativeLayout>

                ...

                <RecyclerView>

                <Button>

            </LinearLayout>
        </android.support.v4.widget.NestedScrollView>
    </FrameLayout>
</android.support.design.widget.CoordinatorLayout>

 

내가 개발하던 앱중 하나의 Activity에서 위와 같은 레이아웃 구조를 가지고 있었다.

 

그런데 NestedScrollView 안에 Button을 눌러서 기능을 실행할때 두번을 눌러야 실행이 되었다.

 

NestedScrollView안의 내용이 짧아서 스크롤이 필요없이 모든 view가 보인다면 두번 누를필요없이 바로 실행이 된다.

 

단. 내용이 길어져서 Button이 스크롤해야 보이게 되는 경우, 무조건 두번을 눌러야한다.

 

검색을 해보다 지쳐가던중, 한개의 게시글을 발견했다.

 

https://stackoverflow.com/questions/31829976/onclick-method-not-working-properly-after-nestedscrollview-scrolled

 

사용자 Mihuilk이 쓴 내용을 보면 AppBarLayout의 Behavior를 입혀서 위의 문제점을 해결할 수 있다는 것이다.

 

그 안의 링크 https://issuetracker.google.com/issues/66996774 에서 #44를 보면

 

At such point the SCROLL_STATE_IDLE is received after a short delay (this delay is the actual problem). So this means that after the scroll has "visually" ended, since the RecyclerVIew doesn't have yet the idle scroll state due to such delay, touch events do not work on the list items until the RecyclerView gets into the idle state,.

 

시각적으로 끝에 도달하여 RecyclerView가 idle상태로 바뀐것으로 보이지만 실제로는 그렇지 않아서 터치이벤트가 먹히지 않는 다는 것을 알수 있다.

 

그래서 https://gist.github.com/chrisbanes/8391b5adb9ee42180893300850ed02f2 에서

새로운 Behavior를 설정해줄 때, stopNestedScroll()을 통해 스크롤을 멈추고 터치이벤트가 정상적으로 들어올수 있도록 한다.

 

이와같은 설명이 잘못되었을 수도 있지만 지금은 이렇게 이해하고 있다.