Use width of swipe action text to calculate maximum swipe distance

This commit is contained in:
cketti 2022-11-04 15:11:06 +01:00
parent f75101dfc1
commit ebb54c26cd
3 changed files with 20 additions and 4 deletions

View file

@ -27,12 +27,16 @@ class MessageListSwipeCallback(
private val adapter: MessageListAdapter,
private val listener: MessageListSwipeListener
) : ItemTouchHelper.Callback() {
private val swipePadding = context.resources.getDimension(R.dimen.messageListSwipeIconPadding).toInt()
private val swipeThreshold = context.resources.getDimension(R.dimen.messageListSwipeThreshold)
private val backgroundColorPaint = Paint()
private val swipeRightLayout: View
private val swipeLeftLayout: View
private var maxSwipeRightDistance: Int = -1
private var maxSwipeLeftDistance: Int = -1
init {
val layoutInflater = LayoutInflater.from(context)
@ -166,13 +170,23 @@ class MessageListSwipeCallback(
val heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
swipeLayout.measure(widthMeasureSpec, heightMeasureSpec)
swipeLayout.layout(0, 0, width, height)
if (swipeRight) {
maxSwipeRightDistance = textView.right + swipePadding
} else {
maxSwipeLeftDistance = swipeLayout.width - textView.left + swipePadding
}
}
swipeLayout.draw(this)
}
override fun getMaxSwipeDistance(recyclerView: RecyclerView, direction: Int): Int {
return recyclerView.width / 2
return when (direction) {
ItemTouchHelper.RIGHT -> if (maxSwipeRightDistance > 0) maxSwipeRightDistance else recyclerView.width
ItemTouchHelper.LEFT -> if (maxSwipeLeftDistance > 0) maxSwipeLeftDistance else recyclerView.width
else -> recyclerView.width
}
}
override fun shouldAnimateOut(direction: Int): Boolean {

View file

@ -26,7 +26,7 @@
<TextView
android:id="@+id/swipe_action_text"
android:layout_width="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
@ -37,8 +37,9 @@
android:maxLines="1"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="?attr/messageListSwipeIconTint"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/swipe_action_icon"
app:layout_constraintTop_toTopOf="parent"

View file

@ -26,7 +26,7 @@
<TextView
android:id="@+id/swipe_action_text"
android:layout_width="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/messageListSwipeTextPadding"
android:layout_marginTop="16dp"
@ -37,6 +37,7 @@
android:maxLines="1"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="?attr/messageListSwipeIconTint"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toRightOf="@+id/swipe_action_icon"