当前位置: 首页 > 知识库问答 >
问题:

带有滑动操作的列表元素-按钮不可点击

姚文轩
2023-03-14

我有一个包含三个滑动操作的列表项,如下所示:

常规列表项和按钮是在xml中定义的两种不同布局。

为了显示按钮操作,我使用了< code > ItemTouchHelper。简单回调。在< code>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
    )
}

这是一个简短的布局文件,演示了我构建用户界面的方式:

<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>

我的问题是按钮不可点击。到目前为止,我尝试过:

    < li >设置高程以将元素置于顶部 < li >根据按钮的可见性状态设置可点击的项目

这可以在布局文件中看到。我想在xml中定义元素,如果可能的话,不要手工绘制它们。

共有1个答案

焦正德
2023-03-14

问题解决了<代码>ItemTouchHelper。SimpleCallback吞下所有触摸事件。因此,您需要为按钮注册一个TouchListener。在我的案例中,按钮来自xml。受此启发,我提出了以下解决方案:

@SuppressLint("ClickableViewAccessibility")
class NachrichtItemSwipeCallback(private val recyclerView: RecyclerView) :
    ItemTouchHelper.SimpleCallback(0, LEFT) {
    private val itemTouchHelper: ItemTouchHelper

    private var binding: ListItemNachrichtBinding? = null
    private var lastSwipedPosition: Int = -1

    init {
        // Disable animations as they don't work with custom list actions
        (this.recyclerView.itemAnimator as? SimpleItemAnimator)?.supportsChangeAnimations = false

        this.recyclerView.setOnTouchListener { _, touchEvent ->
            if (lastSwipedPosition < 0) return@setOnTouchListener false
            if (touchEvent.action == MotionEvent.ACTION_DOWN) {
                val viewHolder =
                    this.recyclerView.findViewHolderForAdapterPosition(lastSwipedPosition)
                val swipedItem: View = viewHolder?.itemView ?: return@setOnTouchListener false
                val rect = Rect()
                swipedItem.getGlobalVisibleRect(rect)

                val point = Point(touchEvent.rawX.toInt(), touchEvent.rawY.toInt())
                if (rect.top < point.y && rect.bottom > point.y) {
                    // Consume touch event directly
                    val buttons =
                        binding?.buttonActionBar?.children
                            .orEmpty()
                            .filter { it.isClickable }
                            .toList()

                    val consumed = consumeTouchEvents(buttons, point.x, point.y)
                    if (consumed) {
                        animateClosing(binding?.nachrichtListItem)
                    }
                    return@setOnTouchListener false
                }
            }
            return@setOnTouchListener false
        }

        this.itemTouchHelper = ItemTouchHelper(this)
        this.itemTouchHelper.attachToRecyclerView(this.recyclerView)
    }

    // Only for drag & drop functionality
    override fun onMove(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        target: RecyclerView.ViewHolder
    ): Boolean = false

    override fun onChildDraw(
        canvas: Canvas,
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        dX: Float,
        dY: Float,
        actionState: Int,
        isCurrentlyActive: Boolean
    ) {
        binding = (viewHolder as? NachrichtViewHolder)?.binding
        val foreground = binding?.nachrichtListItem
        val background = binding?.buttonActionBar

        val backgroundWidth = background?.measuredWidth?.toFloat()

        // only draw until start of action buttons
        val x: Float = when {
            dX.absoluteValue > backgroundWidth ?: dX -> backgroundWidth?.unaryMinus() ?: dX
            else -> dX
        }

        foreground?.translationX = x
    }

    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
        this.lastSwipedPosition = viewHolder.adapterPosition
        recyclerView.adapter?.notifyItemChanged(this.lastSwipedPosition)
    }

    private fun animateClosing(
        foreground: ConstraintLayout?
    ) {
        foreground ?: return
        ObjectAnimator.ofFloat(foreground, "translationX", 0f).apply {
            duration = DURATION_ANIMATION
            start()
        }.doOnEnd { applyUiWorkaround() }
    }

    // See more at https://stackoverflow.com/a/37342327/3734116
    private fun applyUiWorkaround() {
        itemTouchHelper.attachToRecyclerView(null)
        itemTouchHelper.attachToRecyclerView(recyclerView)
    }

    private fun consumeTouchEvents(
        views: List<View?>,
        x: Int,
        y: Int
    ): Boolean {
        views.forEach { view: View? ->
            val viewRect = Rect()
            view?.getGlobalVisibleRect(viewRect)

            if (viewRect.contains(x, y)) {
                view?.performClick()
                return true
            }
        }
        return false
    }

    companion object {
        private const val DURATION_ANIMATION: Long = 250
    }
}
 类似资料:
  • 滑动操作列表是列表的扩展,它提供滑动操作的功能,滑动列表元素可以展现隐藏的功能菜单,就像滑动删除一样。 让我们来看一下滑动列表元素的布局结构: <div class="list-block"> <ul> <!-- li上额外的“swipeout”类 --> <li class="swipeout"> <!-- 被“swipeout-content”包裹起来的普通列表

  • 列表 列表以单列形式分行展示数据。可使用列表展示动态更改的内容。 列表对象具有以下特性: 支持多行内容展示类型 可滚动 可设置背景色或图片 支持用户交互 在设计阶段指定列表行类型。所有行类型都必须提前设计好。运行时,您可以选择您真正需要的行类型。 行类型使用要一致。您可能会创建不同的行类型来展示您的内容、页眉和页脚等。要确保行类型使用一致。 避免混合使用内容类型截然不同的列表行。当展示内容时,要确

  • 我用selenium和python一起使用,我试图点击一个按钮,但它似乎不起作用。下面是html结构的图片: 这里 我已经尝试通过xpath和类单击该按钮,但没有成功。它也没有给我一个错误。我为每一个答案感到高兴!

  • 我想使用硒选择值“DATE”的选项。关于它的事情是,选项文本设置在另一部分,即ul列表。我尝试了一些我发现的解决方案,但没有一个奏效。这是我的代码: 我也试过这个,但仍然得到同样的错误: 错误: dropdown_trigger = browser.find_element_by_xpath(“//选择[@data-test=”筛选器排序“]') 消息: 元素不可交互: 元素当前不可见,可能无法操

  • lindex 返回名称为key的list中index位置的元素,例如: redis 127.0.0.1:6379> lindex mylist5 0

  • 本文向大家介绍jQGrid Table操作列中点击【操作】按钮弹出按钮层的实现代码,包括了jQGrid Table操作列中点击【操作】按钮弹出按钮层的实现代码的使用技巧和注意事项,需要的朋友参考一下 在使用JqGrid时,Table中最后一列是操作列,在操作列中每一行都一个操作按钮,该操作按钮类似下拉菜单,如下图: 在点击Table中【操作】一列时需要弹出一个Div层,该Div层中包含一堆按钮,用