Skip to content
This repository has been archived by the owner on Mar 27, 2022. It is now read-only.

Commit

Permalink
Fix the problem that did not scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
MinSeungHyun committed Aug 20, 2019
1 parent 1ad7292 commit 67c978e
Showing 1 changed file with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.view.View
import android.view.ViewGroup
import android.widget.ScrollView
import androidx.recyclerview.widget.RecyclerView
import kotlin.math.abs

private class SlideToDeleteTouchListener(
private val container: ViewGroup,
Expand All @@ -18,6 +19,7 @@ private class SlideToDeleteTouchListener(
) : View.OnTouchListener {

private var firstX = 0f
private var firstY = 0f
private var firstViewX = 0f
private var velocity = 0f
private var isSlidedToRight = false
Expand All @@ -26,20 +28,30 @@ private class SlideToDeleteTouchListener(
@SuppressLint("ClickableViewAccessibility")
override fun onTouch(v: View, event: MotionEvent): Boolean {
val x = event.x
val y = event.y
val parentWidth = container.width.toFloat()

when (event.action) {
MotionEvent.ACTION_DOWN -> {
requestDisallowInterceptTouchEvent(true)
firstX = x
firstY = y
firstViewX = content.x
}
MotionEvent.ACTION_MOVE -> {
if (firstViewX != 0f && firstViewX != parentWidth && firstViewX != parentWidth * -1) return true
val distance = x - firstX
var viewX = firstViewX + distance
val distanceX = x - firstX
val distanceY = y - firstY
var viewX = firstViewX + distanceX

if (abs(distanceY) > abs(distanceX) && abs(distanceX) < parentWidth / 20) {
//Vertical scrolling
requestDisallowInterceptTouchEvent(false)
resetView()
} else {
requestDisallowInterceptTouchEvent(true)
}

isSlidedToRight = (distance > 0 || viewX > 0) && firstViewX >= 0
isSlidedToRight = (distanceX > 0 || viewX > 0) && firstViewX >= 0

if (viewX > parentWidth) viewX = parentWidth

Expand Down Expand Up @@ -77,6 +89,11 @@ private class SlideToDeleteTouchListener(
return true
}

private fun resetView() {
autoSlide(FLAG_RESET)
content.alpha = 1f
}

private fun onViewMove() {
var x = content.x
val parentWidth = container.width
Expand Down

0 comments on commit 67c978e

Please sign in to comment.