如何在滚动 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>
PageViewAdapter
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
答案 0 :(得分:0)
仅在不滚动时使recyclerView对单击事件做出反应:
if (recyclerView.scrollState == SCROLL_STATE_IDLE) {
playOrResume()
}
更新
也许这样可以工作:
private static int mScrollState;
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView,int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
scrollState = recyclerView.scrollState;
}
// or use this method for finer control
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
switch (newState) {
case RecyclerView.SCROLL_STATE_IDLE:
System.out.println("The RecyclerView is not scrolling");
break;
case RecyclerView.SCROLL_STATE_DRAGGING:
System.out.println("Scrolling now");
break;
case RecyclerView.SCROLL_STATE_SETTLING:
System.out.println("Scroll Settling");
break;
}
}
});
使用mScrollState
内的ClickListener
检查状态
更新
因此,onCLick
在onScrollStateChanged
之前被调用,请在Handler
中使用此onCLick
,它将暂停200,这将激活onScrollStateChanged
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (recyclerView.scrollState == SCROLL_STATE_IDLE) {
playOrResume()
}
}
}, 200);
答案 1 :(得分:0)
我认为您将尝试使用ViewPager2.OnPageChangeCallback中的方法。
val pageChangeCallback = object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
// New page becomes selected.
}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
// Current page is scrolled.
}
override fun onPageScrollStateChanged(state: Int) {
when(state) {
ViewPager2.SCROLL_STATE_DRAGGING -> {
// User begins dragging or a fake drag is started.
}
ViewPager2.SCROLL_STATE_IDLE -> {
// Pager is fully stopped/idle.
}
ViewPager2.SCROLL_STATE_SETTLING -> {
// Pager is automatically settling to the current page.
}
}
}
}
// Register the callback to the pager.
viewPager2.registerOnPageChangeCallback(pageChangeCallback)
// TODO viewPager2.unregisterOnPageChangeCallback(pageChangeCallback)