【问题标题】:RecyclerView - Get position from ViewHolder [duplicate]RecyclerView - 从 ViewHolder 获取位置 [重复]
【发布时间】:2020-12-09 19:31:09
【问题描述】:

我用GridLayoutManager 实现了RecyclerView,类似于图片库。我正在尝试使用this library 在其上实现拖动选择行为。

我想在长按视图支架时激活拖动选择。下面是该库的示例激活码。

touchListener.setIsActive(true, index) // index = start position of the view holder in the grid

问题:

我在 ViewHolder 上实现了OnLongClickListener,但是如何获取视图在网格中的位置?

class ImageViewHolder(view: View) : RecyclerView.ViewHolder(view), View.OnLongClickListener {
    override fun onLongClick(v: View): Boolean {
        ImageFragment.activateDragSelect(0) // Calls the above activation snippet
        return true
    }
}

【问题讨论】:

  • 你试过getAdapterPosition()吗?

标签: android android-recyclerview


【解决方案1】:

您可以将ViewHolder 定位为:

getAdapterPosition()

【讨论】:

    猜你喜欢
    • 2017-08-11
    • 1970-01-01
    • 2019-02-17
    • 2020-05-30
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多