웹뷰에 '당겨서 새로고침' 기능을 적용 경우,

웹 콘텐츠에 가로 스와이프 기능이 들어가 있으면 문제가 발생하는 경우가 있다.

따라서 세로로 스와이프 할때만 새로고침이 되도록 해야 한다.

 

먼저, SwipeRefreshLayout을 확장해서 OnlyVerticalSwipeRefreshLayout 클래스를 생성한다.

OnlyVerticalSwipeRefreshLayout.kt

open class OnlyVerticalSwipeRefreshLayout @JvmOverloads constructor(context: Context, attrs: AttributeSet?=null) :
    SwipeRefreshLayout(context, attrs) {

    private val touchSlop: Int = ViewConfiguration.get(context).scaledTouchSlop
    private var prevX = 0f
    private var declined = false

    override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                prevX = MotionEvent.obtain(event).x
                declined = false // New action
            }
            MotionEvent.ACTION_MOVE -> {
                val eventX = event.x
                val xDiff = Math.abs(eventX - prevX)
                if (declined || xDiff > touchSlop) {
                    declined = true // Memorize
                    return false
                }
            }
        }
        return super.onInterceptTouchEvent(event)
    }
}

 

사용은 SwipeRefreshLayout을 사용할 때와 동일하다.

<com.ahikuya.OnlyVerticalSwipeRefreshLayout
    android:id="@+id/swipe_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:animateLayoutChanges="true"
        android:focusable="true"
        android:focusableInTouchMode="true"/>
</com.ahikuya.OnlyVerticalSwipeRefreshLayout>

 

아래 문서를 참고했다.

https://stackoverflow.com/questions/34136178/swiperefreshlayout-blocking-horizontally-scrolled-recyclerview

+ Recent posts