我在适配器中编写了以下代码.

holder.itemView.setOnClickListener {
    val action = SearchFragmentDirections.actionSearchFragmentToArtistFragment(artist.id)
    Navigation.createNavigateOnClickListener(action)
}

And this is may navigation xml:

 ...
    <fragment android:id="@+id/searchFragment"
          android:name="com.salmanseifian.spotiny.ui.search.SearchFragment"
          android:label="SearchFragment">

    <action android:id="@+id/action_searchFragment_to_artistFragment"
            app:destination="@id/artistFragment"
            app:popUpTo="@+id/searchFragment"/>
</fragment>

但它不会起作用的.有人能帮忙吗?

推荐答案

您使用的是lambda,它本身就是一个点击侦听器.

在你的 case 中使用下面的代码.

holder.itemView.setOnClickListener(
Navigation.createNavigateOnClickListener(R.id.action_searchFragment_to_artistFragment)
)

OR, try this way

holder.itemView.setOnClickListener{ view ->
 view.findNavController().navigate(R.id.action_searchFragment_to_artistFragment)
}

Kotlin相关问答推荐

Compose:LaunchedEffect在密钥更改后不会重新启动

调用即发即忘方法--哪个作用域?

在Kotlin中将String转换为T

仅某些用户出现 DateTimeFormatter 错误

在Kotlin lambda的参数中如何指定函数类型?

Kotlin 基于参数类型的返回类型推断

使用调度程序运行异步 Kotlin 代码

Kotlin 可空泛型

循环中的每个元素都应填充行中可用的所有可用空间

在子类中覆盖 kotlin 运算符扩展函数

如何限制 Kotlin 中的枚举?

Kotlin 条件格式字符串

如何在 Android 的 Fragment 中使用 setUserVisibleHint

Anko 中的水平线性布局

如何在主线程上使用 Kotlin 协程 await()

如何在Kotlin中创建填充空值的通用数组?

Android 上的 Kotlin:将map到list

如何在Kotlin中获得KType?

Java Integer.MAX_VALUE 与 Kotlin Int.MAX_VALUE

在 Kotlin 中编写一个等于 Int.MIN_VALUE 的十六进制整数文字