I have a recyclerview in my Android project that displays media contents in each view. What I'm trying to achieve is that I can play / pause media when I scroll up and down. I need to get the position of the adapter fully visible. I am doing something like this.
In my activity snippet, I have the following:
layoutmanager = new LinearLayoutManager(Activity); adapter = new FeedAdapter(vid, userName, this.Context); feeditem.SetLayoutManager(layoutmanager); feeditem.SetAdapter(adapter); var onScrollListener = new XamarinRecyclerViewOnScrollListener(Activity, layoutmanager, adapter);
The scroll listening event is as follows:
public override void OnScrollStateChanged(RecyclerView recyclerView, int newState) { base.OnScrollStateChanged(recyclerView, newState); if (newState == (int)ScrollState.Idle) { layoutmanager = (LinearLayoutManager)recyclerView.GetLayoutManager(); int firstVisiblePosition = layoutmanager.FindFirstCompletelyVisibleItemPosition(); int visible = layoutmanager.FindFirstVisibleItemPosition(); int last = layoutmanager.FindLastVisibleItemPosition(); if (firstVisiblePosition >= 0) { if (oldFocusedLayout != null) { Toast.MakeText(ctx, "Stop Video", ToastLength.Long).Show(); } } currentFocusedLayout = layoutmanager.FindViewByPosition(firstVisiblePosition); Toast.MakeText(ctx, "Play video", ToastLength.Long).Show(); oldFocusedLayout = currentFocusedLayout; } } feeditem.AddOnScrollListener(onScrollListener);
The problem is that the linearlayout dispatcher method FindFirstCompletelyVisibleItemPosition always returns -1, even if the view is fully visible. Other methods, such as FindFirstVisibleItemPosition and FindLastVisibleItemPosition , give the correct view position.
Any idea what could be the problem here?
source share