Reputation: 4140
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. 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)
}
}
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)
}
Upvotes: 2
Views: 755
Reputation: 4694
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)
}
Upvotes: 5