layoutmanager.FindFirstCompletelyVisibleItemPosition始终返回-1 [英] layoutmanager.FindFirstCompletelyVisibleItemPosition always returns -1

查看:1163
本文介绍了layoutmanager.FindFirstCompletelyVisibleItemPosition始终返回-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的android项目中有一个recyclerview,它在每个视图中显示媒体内容.我想要达到的目的是当我上下滚动时能够播放/暂停媒体.我需要获得完全可见的视图的适配器位置.我正在做这样的事情.

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);

滚动侦听器事件如下:

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);

问题在于,即使视图是完全可见的,线性布局管理器方法FindFirstCompletelyVisibleItemPosition始终返回-1.其他方法,例如FindFirstVisibleItemPositionFindLastVisibleItemPosition给出了视图的正确位置.

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?

推荐答案

layoutManager.findFirstCompletelyVisibleItemPosition()

layoutManager.findFirstCompletelyVisibleItemPosition()

FROM 文档 a>

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.

这意味着,至少一个列表项视图应该是完全可见的,否则,它给出-1(NO_POSITION)

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

从测试

这将起作用并给出正确的位置...

This will work and give correct position...

这将不起作用并给出-1(NO_POSITION),因为两个ListItem视图不完全可见.

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

这篇关于layoutmanager.FindFirstCompletelyVisibleItemPosition始终返回-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆