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.
- RecyclerView should only scroll on vertical scrolling.
- 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)
}
}
- 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)
}