如何防止在ViewPager2中滚动时调用“onClick” ?
请注意我询问 ViewPager2。
当我滚动查看寻呼机时,单击了一个项目。
activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/pager"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"/>
</RelativeLayout>
页面视图适配器
class PageViewAdapter(
private val context: Context,
private val list: ArrayList<Video>,
private val lifecycle: Lifecycle
) :
RecyclerView.Adapter<PageViewAdapter.MyViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val view =
LayoutInflater.from(context).inflate(R.layout.fragment_video, parent, false)
return MyViewHolder(view).apply {
playerView.setOnClickListener {
playOrResume()
}
}
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val mediaSource = buildMediaSource(list[position].uri)
holder.player.prepare(mediaSource)
}
override fun getItemCount(): Int {
return list.size
}
inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val playerView: PlayerView = itemView.player_view
val player: SimpleExoPlayer = ExoPlayerFactory.newSimpleInstance(
context,
DefaultRenderersFactory(context),
DefaultTrackSelector(),
DefaultLoadControl()
)
fun playOrResume() {
player.playWhenReady = !player.playWhenReady
}
}
}
在这里您可以观看动作视频: https ://vimeo.com/340232497