将OnItemLongClickListener添加到groupie回收者视图

时间:2018-10-04 19:58:39

标签: android android-recyclerview

我正在尝试为我的网上论坛回收者列表实现长按侦听器,但是我不断收到错误消息,它期望Boolean并发现unit。有人可以指出正确的方向吗?

适配器的代码:

 private fun updateRecyclerView(items: List<Item>) {
    fun init() {
        recycler_view_people.apply {
            layoutManager = LinearLayoutManager(this@PeopleFragment.context)
            adapter = GroupAdapter<ViewHolder>().apply {
                peopleSection = Section(items)
                add(peopleSection)
                setOnItemClickListener(onItemClick)
                setOnItemLongClickListener(onLongItemClick)
            }
        }
        shouldInitRecyclerView = false
    }

    fun updateItems() = peopleSection.update(items)

    if (shouldInitRecyclerView)
        init()
    else
        updateItems()
}
private val onItemClick = OnItemClickListener { item, view ->
    if (item is ModuleItem) {
        startActivity<ChatActivity>(
                AppConstants.GROUP_NAME to item.module.Code,
                AppConstants.GROUP_ID to item.modId
        )


    }
}
private val onLongItemClick = OnItemLongClickListener { item, view ->


}

image

2 个答案:

答案 0 :(得分:1)

您应该在监听器中声明布尔值的返回类型。

abstract boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)

  

单击并按住此视图中的某个项目时将调用的回调方法。

答案 1 :(得分:1)

您没有犯任何错误,问题在于该方法需要一个函数才能工作并返回,因此您只需要该方法中的一些数据即可。

private val onLongItemClick = OnItemLongClickListener { item, view ->
    val builder = AlertDialog.Builder(this@PeopleFragment.context!!)
    builder.setMessage("Blah").show()
    true
}