【发布时间】:2020-07-20 13:14:01
【问题描述】:
我有一个包含三个滑动操作的列表项,如下所示:
常规列表项和按钮是xml中定义的两种不同布局。
为了显示我使用ItemTouchHelper.SimpleCallback 的按钮操作。在onChildDraw 中,我告诉项目列表项的 x 轴仅在达到按钮控件的宽度之前绘制。
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
)
}
这是一个简短的布局文件,展示了我构建 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>
我的问题是按钮不可点击。 到目前为止我尝试了什么:
- 设置高度以将元素置于顶部
- 根据按钮的可见性状态设置可点击的项目
这可以在布局文件中看到。我想在 xml 中定义元素,如果可能的话不要手动绘制它们。
【问题讨论】:
标签: android android-recyclerview swipe-gesture itemtouchhelper