Android开发者Canary 3.4,kotlin。
发现View.OnLongClickListener提供的类型不匹配。 Android中新的导航图是否不考虑View.OnLongClickListener?
private fun createOnClickListener(stationId: String): View.OnClickListener
{
return View.OnClickListener {
val direction = StationListFragmentDirections.ActionStationListFragmentToStationDetailFragment(stationId)
it.findNavController().navigate(direction)
}
}
private fun createOnLongClickListener(stationId: String, kindId: String): View.OnLongClickListener
{
return View.OnLongClickListener {
val direction = StationListFragmentDirections.ActionStationListFragmentToUpdatePriceFragment(stationId,kindId)
it.findNavController().navigate(direction) // <--- Gives error here
}
}
以上两个函数的行为应相同,但下一个函数(createOnLongClickListener)给出“方向”的“类型不匹配”错误。
是否不支持为View.OnLongClickListener添加导航?
答案 0 :(得分:1)
View.OnLongClickListener需要返回类型为布尔值
示例:
val longClick = View.OnLongClickListener {
return@OnLongClickListener true
}
答案 1 :(得分:0)
找到了答案。如果处理了请求,则OnLongClickListener实际上需要响应:
private fun createOnLongClickListener(stationId: String, kindId: String): View.OnLongClickListener
{
return View.OnLongClickListener {
val direction = StationListFragmentDirections.ActionStationListFragmentToUpdatePriceFragment(stationId,kindId)
it.findNavController().navigate(direction)
true // <--- Add true here to confirm it is handled
}
}
...科特林奇观...;)