layoutmanager.FindFirstCompletelyVisibleItemPosition always returns -1

感情迁移 提交于 2020-01-12 17:24:01

问题


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?


回答1:


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...

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



来源:https://stackoverflow.com/questions/37363901/layoutmanager-findfirstcompletelyvisibleitemposition-always-returns-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!