Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

layoutmanager.FindFirstCompletelyVisibleItemPosition always returns -1

I have a recyclerview in my android project which displays media contents within each view. What I'm trying to achieve is that I'm able to play/pause media as I scroll up and down. I need to get the adapter position of the completely visible view. I'm doing something like this.

In my activity fragment I have this:

        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 listener event looks like this:

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 issue is that the linearlayout manager method FindFirstCompletelyVisibleItemPosition always returns -1 even when the view is completely visible. Other methods like FindFirstVisibleItemPosition and FindLastVisibleItemPosition gives the correct position of the view.

Any idea what might be the issue here?

like image 684
Ahmed Mujtaba Avatar asked May 21 '16 14:05

Ahmed Mujtaba


1 Answers

layoutManager.findFirstCompletelyVisibleItemPosition()

FROM DOCUMENT

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

It mean that, at least one listitem view should be fully visible otherwise, it give -1 (NO_POSITION)

FROM TESTING

This will work and give correct position...

Fully Visible ListItem View

This won't work and give -1 (NO_POSITION), because two ListItem view is not fully visible.

enter image description here

like image 120
Jongz Puangput Avatar answered Oct 31 '22 21:10

Jongz Puangput