简体   繁体   English

垂直方向的 Recyclerview 在屏幕的水平滚动上上下滚动

[英]Recyclerview with vertical orientation scrolls up and down on horizontally scrolls of the Screen

I need to create a list with full screen videos.I used PagerSnapHelper for full screen child item.我需要创建一个包含全屏视频的列表。我将 PagerSnapHelper 用于全屏子项。 I have a single instance of Exoplayer and change video on scroll.我有一个 Exoplayer 实例并在滚动时更改视频。 Video also change on horizontally scroll.视频也会在水平滚动时发生变化。

RecyclerView.OnScrollListener() methods also called on horizontal scrolling. RecyclerView.OnScrollListener() 方法也调用水平滚动。

Please suggest how I resolve these Issues.请建议我如何解决这些问题。

  1. RecyclerView should only scroll on vertical scrolling. RecyclerView 应该只在垂直滚动时滚动。
  2. RecyclerView.OnScrollListener() should not call on horizontal scrolling RecyclerView.OnScrollListener() 不应调用水平滚动

1. Scrolling Listener 1. 滚动监听

class SnapOnScrollListener(
private val snapHelper: SnapHelper,
var behavior: Behavior = Behavior.NOTIFY_ON_SCROLL,
var onSnapPositionChangeListener: OnSnapPositionChangeListener? = null
) : RecyclerView.OnScrollListener() {

enum class Behavior {
    NOTIFY_ON_SCROLL,
    NOTIFY_ON_SCROLL_STATE_IDLE
}


override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
    System.out.println("dx.. $dx  dy $dy")
    if (behavior == Behavior.NOTIFY_ON_SCROLL) {
        maybeNotifySnapPositionChange(recyclerView)
        System.out.println("dx.. $dx  dy $dy snapChange")
    }
}

override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
    System.out.println("dx......")

    if ( behavior == Behavior.NOTIFY_ON_SCROLL_STATE_IDLE
        && newState == RecyclerView.SCROLL_STATE_IDLE
    ) {
        maybeNotifySnapPositionChange(recyclerView)
        System.out.println("dx......SnapChange")

    }
}

private fun maybeNotifySnapPositionChange(recyclerView: RecyclerView) {
    val snapPosition = snapHelper.getSnapPosition(recyclerView)
    val snapPositionChanged =
        (snapPosition != RecyclerView.NO_POSITION)
    if (snapPositionChanged) {
        onSnapPositionChangeListener?.onSnapPositionChange(snapPosition)

    }
}

fun SnapHelper.getSnapPosition(recyclerView: RecyclerView): Int {
    val layoutManager = recyclerView.layoutManager ?: return RecyclerView.NO_POSITION
    val snapView = findSnapView(layoutManager) ?: return RecyclerView.NO_POSITION
    return layoutManager.getPosition(snapView)
}
}
  1. RecyclerView Adapter RecyclerView 适配器
private fun manageLiveShowList() {
    liveShowAdapter = LiveShowAdapter(List.productList)
    val layoutManager = LinearLayoutManager(requireContext(), RecyclerView.VERTICAL, false)
    mLiveShowRecycler?.layoutManager = layoutManager
    mLiveShowRecycler?.adapter = liveShowAdapter
    snapHelper = PagerSnapHelper()
    snapHelper.attachToRecyclerView(mLiveShowRecycler)
    manageScrollListener(snapHelper)
}

3. Add Scroll Listener 3.添加滚动监听

   private fun manageScrollListener(snapHelper: SnapHelper) {
    val snapOnScrollListener = SnapOnScrollListener(
        snapHelper,
        SnapOnScrollListener.Behavior.NOTIFY_ON_SCROLL,
        this
    )
    mLiveShowRecycler?.addOnScrollListener(snapOnScrollListener)
}

To disable scrolling of the RecyclerView you can modify layout manager as it has canScrollVertically and canScrollHorizontally methods.要禁用RecyclerView的滚动,您可以修改布局管理器,因为它具有canScrollVerticallycanScrollHorizontally方法。 Note: the content inside of the view holders in recycler view can be scrollable by itself.注意:回收器视图中视图持有者内部的内容可以自行滚动。 That is another issue.那是另一个问题。

In your case override canScrollHorizontally to always return false :在您的情况下,覆盖canScrollHorizontally以始终返回false

private fun manageLiveShowList() {
    liveShowAdapter = LiveShowAdapter(List.productList)
    val layoutManager = LinearLayoutManager(requireContext(), RecyclerView.VERTICAL, false) {
            override fun canScrollHorizontally(): Boolean = false
        }
    mLiveShowRecycler?.layoutManager = layoutManager
    mLiveShowRecycler?.adapter = liveShowAdapter
    snapHelper = PagerSnapHelper()
    snapHelper.attachToRecyclerView(mLiveShowRecycler)
    manageScrollListener(snapHelper)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM