Layoutmanager.FindFirstCompletelyVisibleItemPosition always returns -1

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?

+6
source share
1 answer

layoutManager.findFirstCompletelyVisibleItemPosition ()

FROM DOCUMENT

Returns the position of the adapter in the FIRST FULLY VISIBLE view . This item does not include adapter changes that were sent after the last pass of the layout.

This means that at least one view of the listitem must be fully visible otherwise, it gives -1 (NO_POSITION)

FROM TESTING

This will work and give the correct position ...

Fully Visible ListItem View

This will not work and will give -1 (NO_POSITION), because the two ListItem views are not completely visible.

enter image description here

+2
source

All Articles