大型物品的RecyclerView

编程入门 行业动态 更新时间:2024-10-24 16:23:13
本文介绍了大型物品的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

本文发布于:2023-11-30 01:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648229.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:物品   RecyclerView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!