I have a list item with three swipe actions which looks like this:
The regular list item and the buttons are two different layouts defined in xml.
To reveal the button actions I use ItemTouchHelper.SimpleCallback
. In onChildDraw
I tell the item list item's x-axis to be only drawn until it reaches the width of the button controls.
override fun onChildDraw(
c: Canvas,
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
dX: Float,
dY: Float,
actionState: Int,
isCurrentlyActive: Boolean
) {
val foreground = (viewHolder as? NachrichtViewHolder)?.binding?.nachrichtListItem
val background = (viewHolder as? NachrichtViewHolder)?.binding?.background
val x: Float = when {
dX.absoluteValue > background?.measuredWidth?.toFloat() ?: dX -> background?.measuredWidth?.toFloat()
?.unaryMinus() ?: dX
else -> dX
}
getDefaultUIUtil().onDraw(
c,
recyclerView,
foreground,
x,
dY,
actionState,
isCurrentlyActive
)
}
Here is an abbreviated layout file demonstrating the way I built the ui:
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/background"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:clickable="@{backgroundVisible}"
android:focusable="@{backgroundVisible}"
android:focusableInTouchMode="@{backgroundVisible}"
android:elevation="@{backgroundVisible ? 4 : 0}">
<ImageButton
android:id="@+id/actionReply"/>
<ImageButton
android:id="@+id/actionShare"/>
<ImageButton
android:id="@+id/actionDelete"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/nachrichtListItem"
android:elevation="@{backgroundVisible ? 0 : 4}"
android:clickable="@{!backgroundVisible}"
android:focusable="@{!backgroundVisible}"
android:focusableInTouchMode="@{!backgroundVisible}">
<!-- regular list item -->
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
My problem is that the buttons are not clickable. What I tried so far:
- set elevation to bring element on top
- set items clickable depending on the visibility state of the buttons
This can be seen in the layout file. I want to define the elements inside xml and not draw them manually if possible.
The problem is solved.
ItemTouchHelper.SimpleCallback
swallows all your touch events. So you need to register aTouchListener
for the buttons. The buttons come in my case from xml. Inspired by this I came up with the following solution: