1

如何防止在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

4

2 回答 2

1

仅在不滚动时使 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使用它,它将停止 200 并且将启用激活HandleronCLickonScrollStateChanged

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    if (recyclerView.scrollState == SCROLL_STATE_IDLE) {
        playOrResume()
    }
  }
}, 200);
于 2019-06-04T15:52:03.747 回答
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)
于 2019-06-06T13:14:39.463 回答