简体   繁体   中英

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. I have a single instance of Exoplayer and change video on scroll. Video also change on horizontally scroll.

RecyclerView.OnScrollListener() methods also called on horizontal scrolling.

Please suggest how I resolve these Issues.

  1. RecyclerView should only scroll on vertical scrolling.
  2. RecyclerView.OnScrollListener() should not call on horizontal scrolling

1. Scrolling Listener

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
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

   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. 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 :

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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