【发布时间】:2018-01-09 12:33:11
【问题描述】:
在我的应用程序中,我使用的是RecyclerView,它应该在用户向下滚动时继续加载图像。但这给我带来了一个我无法解决的问题。基本上,我说的是
loadPhotos.LoadMoreItems(3);
这意味着,首先加载 3 张照片。 一旦达到某个点(通过滚动)继续加载另外 3 张图片(组成一行):
var onScrollListener = new XamarinRecyclerViewOnScrollListener(mLayoutManager);
onScrollListener.LoadMoreEvent += (object sender, EventArgs e) =>
{
int oldCount = loadPhotos.pictures.Count;
loadPhotos.LoadMoreItems(3);
mAdapter.NotifyItemRangeChanged(oldCount - 1, loadPhotos.pictures.Count);
};
如果我只加载 3 张照片,首先,手机会加载 3 张照片,然后一瞬间,它会加载下一行,因为已到达该位置。所以这确实有效,但如果我开始滚动我会收到这个错误:
“cannot call this method while RecyclerView is computing a layout or scrolling”
在这一行:
mAdapter.NotifyItemRangeChanged(oldCount - 1, loadPhotos.pictures.Count);
另一个有趣的事实是,我可以让它在小型手机上运行,但屏幕更大的手机(三星 s8)会更快崩溃。
所以,我真的很困惑如何做到这一点。作为额外信息,这是我的全部活动:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Fragment_Gallery);
galleryType = Intent.GetIntExtra("galleryType", 0);
picturesinarow = Intent.GetIntExtra("picturesinarow", 1);
recyclerLayout = Intent.GetIntExtra("recyclerLaoyut", (int)RecyclerLayout.Multiplepics);
hasHeader = Intent.GetBooleanExtra("hasHeader", false);
// Instantiate the photo album:
loadPhotos = new BookOfLife.CustomRecyclerView.ReloadAdapter(this.galleryType);
loadPhotos.LoadMoreItems(3);//(Constants.ITEMSPERPAGE);
// Get our RecyclerView layout:
mRecyclerView = FindViewById<RecyclerView>(Resource.Id.recyclerView);
//............................................................
// Layout Manager Setup:
// Use the built-in linear layout manager:
//mLayoutManager = new LinearLayoutManager(this);
// Or use the built-in grid layout manager (two horizontal rows):
mLayoutManager = new GridLayoutManager(this, this.picturesinarow, GridLayoutManager.Vertical, false);
// Plug the layout manager into the RecyclerView:
mRecyclerView.SetLayoutManager(mLayoutManager);
//............................................................
// Adapter Setup:
// Create an adapter for the RecyclerView, and pass it the
// data set (the photo album) to manage:
mAdapter = new PhotoAlbumAdapter(loadPhotos, this.picturesinarow, (RecyclerLayout)this.recyclerLayout, this.hasHeader);
// Register the item click handler (below) with the adapter:
mAdapter.ItemClick += OnItemClick;
// Plug the adapter into the RecyclerView:
mRecyclerView.SetAdapter(mAdapter);
//.........................................................................
//Set Spanwidth for GridHeader
mLayoutManager.SetSpanSizeLookup(new GridViewSpansizeLookup(mAdapter, mLayoutManager));
// On Scroll Listener
var onScrollListener = new XamarinRecyclerViewOnScrollListener(mLayoutManager);
onScrollListener.LoadMoreEvent += (object sender, EventArgs e) =>
{
int oldCount = loadPhotos.pictures.Count;
loadPhotos.LoadMoreItems(3);//(Constants.LOADNEXTITEMSTHRESHHOLD);
mAdapter.NotifyItemRangeChanged(oldCount - 1, loadPhotos.pictures.Count);
};
mRecyclerView.AddOnScrollListener(onScrollListener);
}
希望你们能帮帮我:)
【问题讨论】:
-
您正在同步加载图像,这就是为什么屏幕较小的手机能够显示您的内容,但在较大的屏幕上,您的代码在完成读取之前的图像之前就被调用了。您可以使用 BlockingCollection 异步加载图像,然后显示它们。生产者/消费者模式。
-
好吧听起来很合理。但我真的不知道如何在我的问题中使用它
-
某种形式的虚拟化集合。并研究生产者/消费者模式。这似乎是您问题的准确答案。
-
谢谢!不过我现在确实找到了另一种简单的方法:)
标签: android xamarin android-recyclerview xamarin.android