大型物品的RecyclerView [英] RecyclerView for large items

查看:72
本文介绍了大型物品的RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的应用程序中,我们有一个RecyclerView,可在水平列表中显示大项目.我们使用LinearLayoutManager来做到这一点.当用户滚动屏幕时,会发生此问题.当下一个大项目需要出现在屏幕上时,UI被冻结.

In our app we have a RecyclerView that shows large items in horizontal list. We use LinearLayoutManager to do that. The issue occurs when user scrolls the screen. When next large items needs to appear on a screen UI is frozen.

我们要实现的是对RecyclerView项目的某种延迟加载.这样类似于facebook的实现用户看到一个存根,并且一旦他停止滚动UI就会更新以显示实际内容.

What we wanted to implement is some kind of lazy loading for RecyclerView item. So that similar to facebook's implementation user saw an stub, and once he stopped scrolling UI would update to show actual content.

问题是-正确的扩展点是什么?我应该实现自定义LayoutManager吗?还是有现有的解决方案?

The question is - what is the correct extension point for that? Should I implement custom LayoutManager? Or there are existing solutions for that?

推荐答案

如果您使用Xamarin作为标签,则可以执行以下操作:

If you are using Xamarin as your tag say you can do this:

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

    private LinearLayoutManager LayoutManager;

    public XamarinRecyclerViewOnScrollListener (LinearLayoutManager layoutManager)
    {
        LayoutManager = layoutManager;
    }

    public override void OnScrolled (RecyclerView recyclerView, int dx, int dy)
    {
        base.OnScrolled (recyclerView, dx, dy);

        var visibleItemCount = recyclerView.ChildCount;
        var totalItemCount = recyclerView.GetAdapter().ItemCount;
        var pastVisiblesItems = LayoutManager.FindFirstVisibleItemPosition();

        if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
            LoadMoreEvent (this, null);
        }
    }
}

要在您的视图中使用它,请使用:

To use this in your view use:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        var view = base.OnCreateView(inflater, container, savedInstanceState);

        var recyclerView = view.FindViewById<RecyclerView>(Resource.Id.my_recycler_view);
        if (recyclerView != null)
        {
            recyclerView.HasFixedSize = true;

            var layoutManager = new LinearLayoutManager(Activity);

            var onScrollListener = new XamarinRecyclerViewOnScrollListener (layoutManager);
            onScrollListener.LoadMoreEvent += (object sender, EventArgs e) => {
                //Load more stuff here
            };

            recyclerView.AddOnScrollListener (onScrollListener);

            recyclerView.SetLayoutManager(layoutManager);
        }
        return view;
    }

这篇关于大型物品的RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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