parent
d8ccc85a94
commit
038ab21f34
@ -0,0 +1,68 @@ |
|||||||
|
package net.pokeranalytics.android.ui.helpers |
||||||
|
|
||||||
|
import android.graphics.Canvas |
||||||
|
import android.view.View |
||||||
|
import androidx.core.view.isVisible |
||||||
|
import androidx.recyclerview.widget.ItemTouchHelper |
||||||
|
import androidx.recyclerview.widget.RecyclerView |
||||||
|
import net.pokeranalytics.android.R |
||||||
|
import net.pokeranalytics.android.ui.adapter.RowRepresentableAdapter |
||||||
|
|
||||||
|
/** |
||||||
|
* Swipe to delete callback |
||||||
|
*/ |
||||||
|
class SwipeToDeleteCallback(var adapter: RowRepresentableAdapter, var onDelete: ((position: Int) -> Unit)? = null) : |
||||||
|
ItemTouchHelper.SimpleCallback(ItemTouchHelper.ACTION_STATE_IDLE, ItemTouchHelper.START or ItemTouchHelper.END) { |
||||||
|
|
||||||
|
override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean { |
||||||
|
return false |
||||||
|
} |
||||||
|
|
||||||
|
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) { |
||||||
|
val position = viewHolder.adapterPosition |
||||||
|
onDelete?.invoke(position) |
||||||
|
} |
||||||
|
|
||||||
|
override fun onChildDraw( |
||||||
|
c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, |
||||||
|
dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean |
||||||
|
) { |
||||||
|
val foregroundView = viewHolder.itemView.findViewById<View?>(net.pokeranalytics.android.R.id.foreground) |
||||||
|
val backgroundView = viewHolder.itemView.findViewById<View?>(net.pokeranalytics.android.R.id.background) |
||||||
|
foregroundView?.let { |
||||||
|
getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX, dY, actionState, isCurrentlyActive) |
||||||
|
backgroundView?.findViewById<View?>(R.id.leftIcon)?.isVisible = dX > 0 |
||||||
|
backgroundView?.findViewById<View?>(R.id.rightIcon)?.isVisible = dX < 0 |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { |
||||||
|
val foregroundView = viewHolder.itemView.findViewById<View?>(net.pokeranalytics.android.R.id.foreground) |
||||||
|
foregroundView?.let { |
||||||
|
getDefaultUIUtil().clearView(foregroundView) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) { |
||||||
|
viewHolder?.let { |
||||||
|
val foregroundView = viewHolder.itemView.findViewById<View?>(net.pokeranalytics.android.R.id.foreground) |
||||||
|
foregroundView?.let { |
||||||
|
getDefaultUIUtil().onSelected(foregroundView) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
override fun onChildDrawOver( |
||||||
|
c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder?, dX: Float, dY: Float, |
||||||
|
actionState: Int, isCurrentlyActive: Boolean |
||||||
|
) { |
||||||
|
viewHolder?.let { |
||||||
|
val foregroundView = viewHolder.itemView.findViewById<View?>(net.pokeranalytics.android.R.id.foreground) |
||||||
|
foregroundView?.let { |
||||||
|
getDefaultUIUtil().onDrawOver(c, recyclerView, foregroundView, dX, dY, actionState, isCurrentlyActive) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,31 @@ |
|||||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||||
|
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto" |
||||||
|
android:id="@+id/background" |
||||||
|
android:layout_width="match_parent" |
||||||
|
android:layout_height="match_parent" |
||||||
|
android:layout_marginTop="1dp" |
||||||
|
android:layout_marginBottom="1dp" |
||||||
|
android:background="@color/red"> |
||||||
|
|
||||||
|
<androidx.appcompat.widget.AppCompatImageView |
||||||
|
android:id="@+id/leftIcon" |
||||||
|
android:layout_width="24dp" |
||||||
|
android:layout_height="24dp" |
||||||
|
android:layout_marginStart="16dp" |
||||||
|
app:layout_constraintBottom_toBottomOf="parent" |
||||||
|
app:layout_constraintStart_toStartOf="parent" |
||||||
|
app:layout_constraintTop_toTopOf="parent" |
||||||
|
app:srcCompat="@drawable/ic_outline_delete" /> |
||||||
|
|
||||||
|
<androidx.appcompat.widget.AppCompatImageView |
||||||
|
android:id="@+id/rightIcon" |
||||||
|
android:layout_width="24dp" |
||||||
|
android:layout_height="24dp" |
||||||
|
android:layout_marginEnd="16dp" |
||||||
|
app:layout_constraintBottom_toBottomOf="parent" |
||||||
|
app:layout_constraintEnd_toEndOf="parent" |
||||||
|
app:layout_constraintTop_toTopOf="parent" |
||||||
|
app:srcCompat="@drawable/ic_outline_delete" /> |
||||||
|
|
||||||
|
</androidx.constraintlayout.widget.ConstraintLayout> |
||||||
@ -1,37 +1,47 @@ |
|||||||
<?xml version="1.0" encoding="utf-8"?> |
<?xml version="1.0" encoding="utf-8"?> |
||||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" |
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||||
xmlns:app="http://schemas.android.com/apk/res-auto" |
xmlns:app="http://schemas.android.com/apk/res-auto" |
||||||
xmlns:tools="http://schemas.android.com/tools" |
xmlns:tools="http://schemas.android.com/tools" |
||||||
android:id="@+id/container" |
android:id="@+id/container" |
||||||
android:layout_width="match_parent" |
android:layout_width="match_parent" |
||||||
android:layout_height="48dp" |
android:layout_height="48dp"> |
||||||
android:background="?selectableItemBackground"> |
|
||||||
|
|
||||||
<androidx.appcompat.widget.AppCompatTextView |
<include layout="@layout/layout_swipe_to_delete" /> |
||||||
android:id="@+id/title" |
|
||||||
android:layout_width="0dp" |
|
||||||
android:layout_height="wrap_content" |
|
||||||
android:layout_marginTop="16dp" |
|
||||||
android:layout_marginBottom="16dp" |
|
||||||
android:textSize="16sp" |
|
||||||
app:layout_constraintBottom_toBottomOf="parent" |
|
||||||
app:layout_constraintEnd_toEndOf="@+id/guidelineEnd" |
|
||||||
app:layout_constraintStart_toStartOf="@+id/guidelineStart" |
|
||||||
app:layout_constraintTop_toTopOf="parent" |
|
||||||
tools:text="Data Type Title" /> |
|
||||||
|
|
||||||
<androidx.constraintlayout.widget.Guideline |
<androidx.constraintlayout.widget.ConstraintLayout |
||||||
android:id="@+id/guidelineStart" |
android:id="@+id/foreground" |
||||||
android:layout_width="wrap_content" |
android:layout_width="match_parent" |
||||||
android:layout_height="wrap_content" |
android:layout_height="match_parent" |
||||||
android:orientation="vertical" |
android:background="@color/gray_dark" |
||||||
app:layout_constraintGuide_begin="16dp" /> |
android:foreground="?selectableItemBackground"> |
||||||
|
|
||||||
<androidx.constraintlayout.widget.Guideline |
<androidx.appcompat.widget.AppCompatTextView |
||||||
android:id="@+id/guidelineEnd" |
android:id="@+id/title" |
||||||
android:layout_width="wrap_content" |
android:layout_width="0dp" |
||||||
android:layout_height="wrap_content" |
android:layout_height="wrap_content" |
||||||
android:orientation="vertical" |
android:layout_marginTop="16dp" |
||||||
app:layout_constraintGuide_end="16dp" /> |
android:layout_marginBottom="16dp" |
||||||
|
android:textSize="16sp" |
||||||
|
app:layout_constraintBottom_toBottomOf="parent" |
||||||
|
app:layout_constraintEnd_toEndOf="@+id/guidelineEnd" |
||||||
|
app:layout_constraintStart_toStartOf="@+id/guidelineStart" |
||||||
|
app:layout_constraintTop_toTopOf="parent" |
||||||
|
tools:text="Data Type Title" /> |
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout> |
<androidx.constraintlayout.widget.Guideline |
||||||
|
android:id="@+id/guidelineStart" |
||||||
|
android:layout_width="wrap_content" |
||||||
|
android:layout_height="wrap_content" |
||||||
|
android:orientation="vertical" |
||||||
|
app:layout_constraintGuide_begin="16dp" /> |
||||||
|
|
||||||
|
<androidx.constraintlayout.widget.Guideline |
||||||
|
android:id="@+id/guidelineEnd" |
||||||
|
android:layout_width="wrap_content" |
||||||
|
android:layout_height="wrap_content" |
||||||
|
android:orientation="vertical" |
||||||
|
app:layout_constraintGuide_end="16dp" /> |
||||||
|
|
||||||
|
</androidx.constraintlayout.widget.ConstraintLayout> |
||||||
|
|
||||||
|
</FrameLayout> |
||||||
|
|||||||
Loading…
Reference in new issue