Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GestureDetector with onLongPress #14

Merged
merged 2 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ interface DraggableListener {

fun onPositionChanged(view: View)

fun onLongPress(view: View)

}
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 @@ -7,10 +7,8 @@ import android.animation.AnimatorListenerAdapter
import android.graphics.PixelFormat
import android.os.Build
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
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 @@ -52,6 +50,19 @@ internal fun View.setupDraggable(
val marginEnd = marginEnd()
val marginBottom = marginBottom()

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

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

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 @@ -72,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 @@ -188,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
@@ -1,6 +1,7 @@
package io.github.hyuwah.draggableview

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ArrayAdapter
import android.widget.ImageView
Expand Down Expand Up @@ -30,6 +31,10 @@ class BasicExampleActivity : AppCompatActivity() {
tvLl2.text = "Y: ${view.y.toString().take(6)}"
}
}

override fun onLongPress(view: View) {
toast("Long press view : ${view.id}")
}
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import org.jetbrains.annotations.NotNull;

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 @@ -32,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 @@ -43,4 +46,9 @@ public void onPositionChanged(@NotNull View view) {
TextView textView = findViewById(R.id.tv_coordinate);
textView.setText("X: " + view.getX() + "\tY: " + view.getY());
}

@Override
public void onLongPress(@NonNull View view) {
toast(this,"Long press view : " + view.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.NestedScrollView
import io.github.hyuwah.draggableview.databinding.ActivityScrollingBinding
import io.github.hyuwah.draggableview.utils.toast
import io.github.hyuwah.draggableview.utils.viewBinding
import io.github.hyuwah.draggableviewlib.DraggableListener
import io.github.hyuwah.draggableviewlib.DraggableView
Expand All @@ -26,6 +27,10 @@ class ScrollingActivity : AppCompatActivity() {
Y: ${view.y}
""".trimIndent()
}

override fun onLongPress(view: View) {
toast("Long press view : ${view.id}")
}
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down