当滚动和重新加载同时发生时,RecyclerView崩溃

时间:2018-01-09 12:33:12

标签: android xamarin android-recyclerview xamarin.android

在我的应用程序中,我使用的是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);
    }

希望,你们可以帮助我在这里:)

1 个答案:

答案 0 :(得分:0)

好吧,我似乎找到了一个简单的解决办法:

           if(!mRecyclerView.IsComputingLayout)
            mAdapter.NotifyItemRangeChanged(oldCount - 1, loadPhotos.pictures.Count);

我只需要确保,我总是提供照片,以便recview会滚动。