Skip to content

Commit

Permalink
Add onDragging(dragOffset) callback method #21
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmigloz committed Dec 20, 2018
1 parent ad7a2a6 commit 38cf455
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 28 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
## `1.0.0` (20/12/18)

- Migrate project to Kotlin #22
- Add dragtoclose_ resource prefix #24 (Breaking change)
- Add dragtoclose_ resource prefix #24 (breaking change)
- Add onDragging(dragOffset) callback method #21 (breaking change)

### Migration

Expand All @@ -14,6 +15,8 @@ Before: drag:draggableContainer="@+id/card"
Now: drag:dragtoclose_draggableContainer="@+id/card"
```

Add the new method `onDragging(dragOffset)` to your `DragListener`

## `0.3.1` (26/11/18)

- Card gets stuck if you touch it while it's been closed programmatically #20
Expand Down
35 changes: 18 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ Add the dependency:

```gradle
dependencies {
implementation 'com.github.davidmigloz:drag-to-close:0.1'
implementation 'com.github.davidmigloz:drag-to-close:1.0.0'
}
```

[CHANGELOG](https://github.com/davidmigloz/drag-to-close/blob/master/CHANGELOG.md)

#### Step 3

Use `DragToClose` view group to your layout:
Expand All @@ -38,21 +40,21 @@ Use `DragToClose` view group to your layout:
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:drag="http://schemas.android.com/apk/res-auto"
...
drag:draggableContainer="@+id/card"
drag:draggableView="@+id/close_arrow"
drag:finishActivity="false"
drag:closeOnClick="true">
drag:dragtoclose_draggableContainer="@+id/card"
drag:dragtoclose_draggableView="@+id/close_arrow"
drag:dragtoclose_finishActivity="false"
drag:dragtoclose_closeOnClick="true">

...
</com.davidmiguel.dragtoclose.DragToClose>
```

#### Attributes

- `drag:draggableContainer="[reference]"` (required): view that is going to be dragged (in the example, the card).
- `drag:draggableView="[reference]"` (required): view that is going to listen to draggable events (in the example, the arrow).
- `drag:finishActivity="[true|false]"` (default: `true`): when the `draggableContainer` is dragged out of the `DragToClose` view, the activity is finished (`activity.finish()` is called).
- `drag:closeOnClick="[true|false]"` (default: `false`): when the `draggableView` is clicked, `draggableContainer` is slid down out of the `DragToClose` view automatically (`activity.finish()` is called if need be).
- `drag:dragtoclose_draggableContainer="[reference]"` (required): view that is going to be dragged (in the example, the card).
- `drag:dragtoclose_draggableView="[reference]"` (required): view that is going to listen to draggable events (in the example, the arrow).
- `drag:dragtoclose_finishActivity="[true|false]"` (default: `true`): when the `draggableContainer` is dragged out of the `DragToClose` view, the activity is finished (`activity.finish()` is called).
- `drag:dragtoclose_closeOnClick="[true|false]"` (default: `false`): when the `draggableView` is clicked, `draggableContainer` is slid down out of the `DragToClose` view automatically (`activity.finish()` is called if need be).

#### Methods

Expand All @@ -73,16 +75,15 @@ Use `DragToClose` view group to your layout:
If you want to listen to dragging events, you can use `DragListener`:

- `onStartDraggingView()`: invoked when the view has just started to be dragged.
- `onDragging(dragOffset)`: invoked when the view is being dragged.
- `onViewCosed()`: invoked when the view has being dragged out of the screen and just before calling `activity.finish()` (if need be).

```java
dragToClose.setDragListener(new DragListener() {
@Override
public void onStartDraggingView() {...}

@Override
public void onViewCosed() {...}
});
```kotlin
dragToClose.setDragListener(object : DragListener {
override fun onStartDraggingView() {...}
override fun onDragging(dragOffset: Float) {...}
override fun onViewCosed() {...}
})
```

Take a look at the [sample app](https://github.com/davidmigloz/drag-to-close/tree/master/sample) to see the library working.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ internal class DragHelperCallback(private val dragToClose: DragToClose,
*/
override fun onViewPositionChanged(changedView: View, left: Int, top: Int, dx: Int, dy: Int) {
topBorderDraggableContainer = top
dragToClose.changeDragViewViewAlpha()
dragToClose.onViewPositionChanged()
}

/**
Expand Down
7 changes: 7 additions & 0 deletions lib/src/main/java/com/davidmiguel/dragtoclose/DragListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ interface DragListener {
*/
fun onStartDraggingView()

/**
* Invoked when the view is being dragged.
*
* @param dragOffset vertical drag offset between 0 (start) and 1 (end).
*/
fun onDragging(dragOffset: Float)

/**
* Invoked when the view has being dragged out of the screen
* and just before calling activity.finish().
Expand Down
15 changes: 13 additions & 2 deletions lib/src/main/java/com/davidmiguel/dragtoclose/DragToClose.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.davidmiguel.dragtoclose

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import androidx.annotation.AttrRes
Expand Down Expand Up @@ -99,6 +100,7 @@ class DragToClose : FrameLayout {
* Dispatches touch event to the draggable view.
* The touch is realized only if is over the draggable view.
*/
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent): Boolean {
if (uiBlocked) return true // Ignore event when ui blocked
dragHelper.processTouchEvent(event)
Expand Down Expand Up @@ -222,12 +224,21 @@ class DragToClose : FrameLayout {
}
}

/**
* Invoked when the view position changes.
*/
internal fun onViewPositionChanged() {
val verticalDragOffset = getVerticalDragOffset()
changeDragViewViewAlpha(verticalDragOffset)
listener?.onDragging(verticalDragOffset)
}

/**
* Modify dragged view alpha based on the vertical position while the view is being
* vertical dragged.
*/
internal fun changeDragViewViewAlpha() {
draggableContainer.alpha = 1 - getVerticalDragOffset()
internal fun changeDragViewViewAlpha(verticalDragOffset: Float) {
draggableContainer.alpha = 1 - verticalDragOffset
}

/**
Expand Down
15 changes: 8 additions & 7 deletions sample/src/main/java/com/davidmiguel/sample/Card1Activity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,27 @@ import androidx.appcompat.app.AppCompatActivity
*/
class Card1Activity : AppCompatActivity() {

private val tag = "Card1Activity"

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_card1)

val dragToClose = findViewById<DragToClose>(R.id.drag_to_close)
dragToClose.setDragListener(object : DragListener {
override fun onStartDraggingView() {
Log.d(TAG, "onStartDraggingView()")
Log.d(tag, "onStartDraggingView()")
}

override fun onDragging(dragOffset: Float) {
Log.d(tag, "onDragging(): $dragOffset")
}

override fun onViewCosed() {
Log.d(TAG, "onViewCosed()")
Log.d(tag, "onViewCosed()")
}
})
val btn = findViewById<Button>(R.id.btn)
btn.setOnClickListener { dragToClose.closeDraggableContainer() }
}

companion object {

private val TAG = "Card1Activity"
}
}

0 comments on commit 38cf455

Please sign in to comment.