【问题标题】:Getting the visible item inside the RecyclerView获取 RecyclerView 中的可见项
【发布时间】:2016-08-26 17:09:14
【问题描述】:

我正在使用下面的代码来获取 recyclerview 中的可见项目

在我的片段活动中,我有:

layoutmanager = new LinearLayoutManager(Activity);

        adapter = new FeedAdapter(vid, userName, this.Context);

        feeditem.SetLayoutManager(layoutmanager);
        feeditem.SetAdapter(adapter);

        var onScrollListener = new XamarinRecyclerViewOnScrollListener(Activity, layoutmanager, adapter);

        feeditem.AddOnScrollListener(onScrollListener);

事件处理程序类如下所示:

public class XamarinRecyclerViewOnScrollListener : RecyclerView.OnScrollListener
{
    public delegate void LoadMoreEventHandler(object sender, EventArgs e);

    public event LoadMoreEventHandler LoadMoreEvent;

    private LinearLayoutManager layoutmanager;

    private Action StateChange;

    private FeedAdapter adapter;

    private View currentFocusedLayout, oldFocusedLayout;

    private Context ctx;

    public XamarinRecyclerViewOnScrollListener(LinearLayoutManager layoutManager)
    {
        this.layoutmanager = layoutManager;
    }

    public XamarinRecyclerViewOnScrollListener(Context ctx, LinearLayoutManager layoutmanager, FeedAdapter adapter)
    {
        // TODO: Complete member initialization
        this.layoutmanager = layoutmanager;

        this.adapter = adapter;

        this.ctx = ctx;

        //this.StateChange = StateChange;
    }

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

            if (firstVisiblePosition >= 0)
            {
                if (oldFocusedLayout != null)
                {
                    Toast.MakeText(ctx, "Stop Video", ToastLength.Long).Show();
                }


            }
            currentFocusedLayout = layoutmanager.FindViewByPosition(firstVisiblePosition);
            //VideoView vv_dashboard = (VideoView)currentFocusedLayout.findViewById(R.id.vv_dashboard);
            ////to play video of selected recylerview, videosData is an array-list which is send to recyclerview adapter to fill the view. Here we getting that specific video which is displayed through recyclerview.
            //playVideo(videosData.get(positionView));

            Toast.MakeText(ctx, "Play video", ToastLength.Long).Show();

            oldFocusedLayout = currentFocusedLayout;

        }
    }
}

layoutmanager.FindFirstCompletelyVisibleItemPosition() 总是返回 -1。我想在屏幕上可见的列表中设置项目,然后对其进行处理。我尝试了不同的解决方案,但都没有奏效。实现这一目标的正确方法是什么?

【问题讨论】:

  • 谁能帮忙??

标签: android xamarin android-recyclerview onscrolllistener


【解决方案1】:

如果检测到 -1 表示 RecyclerView 没有检测到布局内的任何项目

【讨论】:

    猜你喜欢
    • 2014-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多