Skip to content

Commit

Permalink
refactor: add conditional to avoid long click listener being triggere…
Browse files Browse the repository at this point in the history
…d everytime the view moves

- also update examples for long click listener usage

fix #13
  • Loading branch information
hyuwah committed Jul 19, 2023
1 parent 2400ae9 commit a92aad7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,17 @@ class DraggableView<T : View> private constructor(

fun setStickyMode(mode: Mode) = apply { this.stickyMode = mode }
fun setAnimated(value: Boolean) = apply { this.animated = value }
fun setListener(listener: DraggableListener?) = apply { this.listener = listener }
fun setListener(listener: DraggableListener?) = apply {
this.listener = listener
if (listener == null) {
targetView.setOnLongClickListener(null)
} else {
targetView.setOnLongClickListener {
listener.onLongPress(it)
true
}
}
}
fun build() = DraggableView(targetView, stickyMode, animated, listener)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.github.hyuwah.draggableviewlib

/**
* State holder class for the draggable view
*/
internal data class DraggableViewState(
var isMoving: Boolean,
var isLongPressRegistered: Boolean,
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.graphics.PixelFormat
import android.os.Build
import android.util.Log
import android.view.*
import androidx.core.view.GestureDetectorCompat
import io.github.hyuwah.draggableviewlib.Draggable.DRAG_TOLERANCE
import io.github.hyuwah.draggableviewlib.Draggable.DURATION_MILLIS
//import io.github.hyuwah.draggableviewlib.DraggableView.StickyRestSide
Expand Down Expand Up @@ -49,14 +50,18 @@ internal fun View.setupDraggable(
val marginEnd = marginEnd()
val marginBottom = marginBottom()


fun longClickSetup(v:View) : GestureDetector {
return GestureDetector(this.context,object : GestureDetector.SimpleOnGestureListener(){
val viewState = DraggableViewState(
isMoving = false,
isLongPressRegistered = false
)
val gestureDetector =
GestureDetectorCompat(this.context, object : GestureDetector.SimpleOnGestureListener() {
override fun onLongPress(e: MotionEvent?) {
draggableListener?.onLongPress(v)
if (viewState.isMoving) return
viewState.isLongPressRegistered = true
draggableListener?.onLongPress(this@setupDraggable)
}
})
}

setOnTouchListener { v, event ->
val viewParent = v.parent as View
Expand All @@ -67,10 +72,11 @@ internal fun View.setupDraggable(
val yMax = parentHeight - v.height - marginBottom
val yMiddle = parentHeight / 2

longClickSetup(v).onTouchEvent(event)
gestureDetector.onTouchEvent(event)

when (event.actionMasked) {
MotionEvent.ACTION_DOWN -> {
viewState.isLongPressRegistered = false
widgetDX = v.x - event.rawX
widgetDY = v.y - event.rawY
widgetInitialX = v.x
Expand All @@ -80,17 +86,20 @@ internal fun View.setupDraggable(
var newX = event.rawX + widgetDX
newX = max(marginStart, newX)
newX = min(xMax, newX)
if (abs(v.x - newX) > DRAG_TOLERANCE) viewState.isMoving = true
v.x = newX

var newY = event.rawY + widgetDY
newY = max(marginTop, newY)
newY = min(yMax, newY)
if (abs(v.y - newY) > DRAG_TOLERANCE) viewState.isMoving = true
v.y = newY

draggableListener?.onPositionChanged(v)
// minimizeBtnListener.onPositionChanged(v, StickyRestSide.HIDE)
}
MotionEvent.ACTION_UP -> {
viewState.isMoving = false
when (stickyAxis) {
DraggableView.Mode.STICKY_X -> {
if (event.rawX >= xMiddle) {
Expand Down Expand Up @@ -196,6 +205,11 @@ internal fun View.setupDraggable(
}
}

if (viewState.isLongPressRegistered) {
// Don't register click
return@setOnTouchListener true
}

if (abs(v.x - widgetInitialX) <= DRAG_TOLERANCE && abs(v.y - widgetInitialY) <= DRAG_TOLERANCE) {
performClick()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import io.github.hyuwah.draggableviewlib.DraggableListener;
import io.github.hyuwah.draggableviewlib.DraggableView;

import static io.github.hyuwah.draggableview.utils.ExtensionsKt.toast;

public class JavaMainActivity extends AppCompatActivity implements DraggableListener {

@Override
Expand All @@ -33,7 +35,7 @@ protected void onCreate(Bundle savedInstanceState) {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(JavaMainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
toast(JavaMainActivity.this, "Button Clicked");
}
});

Expand All @@ -47,6 +49,6 @@ public void onPositionChanged(@NotNull View view) {

@Override
public void onLongPress(@NonNull View view) {

toast(this,"Long press view : " + view.getId());
}
}

0 comments on commit a92aad7

Please sign in to comment.