parent
737a2d4d72
commit
3d7db7370b
@ -0,0 +1,88 @@ |
||||
package io.legado.app.ui.main.bookshelf |
||||
|
||||
import android.content.Context |
||||
import android.gesture.GestureOverlayView.ORIENTATION_HORIZONTAL |
||||
import android.util.AttributeSet |
||||
import android.view.MotionEvent |
||||
import android.view.View |
||||
import android.view.ViewConfiguration |
||||
import android.widget.LinearLayout |
||||
import androidx.viewpager2.widget.ViewPager2 |
||||
import io.legado.app.R |
||||
import io.legado.app.ui.main.MainActivity |
||||
import io.legado.app.utils.activity |
||||
import kotlin.math.absoluteValue |
||||
import kotlin.math.sign |
||||
|
||||
class RootView : LinearLayout { |
||||
|
||||
constructor(context: Context) : super(context) |
||||
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) |
||||
|
||||
private var touchSlop = 0 |
||||
private var initialX = 0f |
||||
private var initialY = 0f |
||||
|
||||
private val parentViewPager: ViewPager2 |
||||
get() { |
||||
return (activity as MainActivity).getViewPager() |
||||
} |
||||
|
||||
private val childViewPager: View get() = findViewById(R.id.view_pager_bookshelf) |
||||
|
||||
init { |
||||
touchSlop = ViewConfiguration.get(context).scaledTouchSlop |
||||
} |
||||
|
||||
private fun canChildScroll(orientation: Int, delta: Float): Boolean { |
||||
val direction = -delta.sign.toInt() |
||||
return when (orientation) { |
||||
0 -> childViewPager.canScrollHorizontally(direction) |
||||
1 -> childViewPager.canScrollVertically(direction) |
||||
else -> throw IllegalArgumentException() |
||||
} |
||||
} |
||||
|
||||
override fun onInterceptTouchEvent(e: MotionEvent): Boolean { |
||||
handleInterceptTouchEvent(e) |
||||
return super.onInterceptTouchEvent(e) |
||||
} |
||||
|
||||
private fun handleInterceptTouchEvent(e: MotionEvent) { |
||||
val orientation = parentViewPager.orientation |
||||
// Early return if child can't scroll in same direction as parent |
||||
if (!canChildScroll(orientation, -1f) && !canChildScroll(orientation, 1f)) { |
||||
return |
||||
} |
||||
|
||||
if (e.action == MotionEvent.ACTION_DOWN) { |
||||
initialX = e.x |
||||
initialY = e.y |
||||
parent.requestDisallowInterceptTouchEvent(true) |
||||
} else if (e.action == MotionEvent.ACTION_MOVE) { |
||||
val dx = e.x - initialX |
||||
val dy = e.y - initialY |
||||
val isVpHorizontal = orientation == ORIENTATION_HORIZONTAL |
||||
// assuming ViewPager2 touch-slop is 2x touch-slop of child |
||||
val scaledDx = dx.absoluteValue * if (isVpHorizontal) .5f else 1f |
||||
val scaledDy = dy.absoluteValue * if (isVpHorizontal) 1f else .5f |
||||
|
||||
if (scaledDx > touchSlop || scaledDy > touchSlop) { |
||||
|
||||
if (isVpHorizontal == (scaledDy > scaledDx)) { |
||||
// Gesture is perpendicular, allow all parents to intercept |
||||
parent.requestDisallowInterceptTouchEvent(false) |
||||
} else { |
||||
// Gesture is parallel, query child if movement in that direction is possible |
||||
if (canChildScroll(orientation, if (isVpHorizontal) dx else dy)) { |
||||
// Child can scroll, disallow all parents to intercept |
||||
parent.requestDisallowInterceptTouchEvent(true) |
||||
} else { |
||||
// Child cannot scroll, allow all parents to intercept |
||||
parent.requestDisallowInterceptTouchEvent(false) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue