简体   繁体   English

在onBindViewHolder中调用notifyDataSetChanged

[英]Calling notifyDataSetChanged inside onBindViewHolder

I'm trying to make a notifyDataSetChanged inside onBindViewHolder once picasso callback has beening triggered but i always have this error 一旦毕加索回调被触发,我正在尝试在onBindViewHolder内进行notifyDataSetChanged,但我总是遇到此错误

java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout

The reason why i need to do that is because i'm using a StaggeredGridLayoutManager so the recyclerview need to be reordered after picasso loaded the images with different sizes 我需要这样做的原因是因为我使用的是StaggeredGridLayoutManager,所以毕加索加载了不同大小的图像后,需要对recyclerview进行重新排序

My actual code is : 我的实际代码是:

@Override
public void onBindViewHolder(filmsAdapter.ViewHolder holder, int position) {


    FilmInfo filmInfo = filmsList.get(position);

    Picasso.get()
            .load(filmInfo.GetPreviewImageUrl())
            .placeholder(R.drawable.ic_launcher)
            .into(holder.getFilmItemIcon(), new Callback() {
                @Override
                public void onSuccess() {
                    notifyDataSetChanged();
                }

                @Override
                public void onError(Exception e) {

                }
            });

        holder.getItemContainer().setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(mContext, MainActivity.class);
            intent.putExtra("filmInfo", filmInfo);
            view.getContext().startActivity(intent);
        }
    });


}

About the problem you can use ReclyclerView.isComputingLayout() to check whenever the RecyclerView is still computing the layout. 关于此问题,您可以使用ReclyclerView.isComputingLayout()来检查RecyclerView是否仍在计算布局。

But, since Picasso loads images asynchronously, it will generate a infinite loop. 但是,由于Picasso异步加载图像,它将生成一个无限循环。 When the Picasso.onSuccess triggers notifyDataSetChanged the onBindViewHolder will be called again, and so on. 当Picasso.onSuccess触发notifyDataSetChanged时,将再次调用onBindViewHolder,依此类推。

The only solution would be if you can keep track of how many images must be loaded or to use a semaphore if you know how many entries must be processed, but if the number of items in the list is undefined then will not be easy as the onBindViewHolder will get called every time the user scrolls the RecyclerView. 唯一的解决方案是,如果您知道必须处理多少个条目,则可以跟踪必须加载多少个图像或使用信号量,但是如果列表中的项目数未定义,那么将变得不容易。每当用户滚动RecyclerView时,都会调用onBindViewHolder。

So, the overall problem is that Picasso.onSuccess will call notifyDataSetChanged, and notifyDataSetChanged will call again onBindViewHolder which will trigger again Picasso. 因此,总体问题是Picasso.onSuccess将调用notifyDataSetChanged,而notifyDataSetChanged将再次调用onBindViewHolder,这将再次触发Picasso。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 notifyDataSetChanged()没有调用onBindViewHolder - notifyDataSetChanged() is not calling onBindViewHolder 从适配器内部调用notifyDataSetChanged() - Calling notifyDataSetChanged() from inside Adapter 从recyclerview适配器内部的arraylist中移除元素,在OnBindViewHolder方法中移除NotifyDatasetChanged - Removing element from arraylist inside recyclerview adapter, NotifyDatasetChanged inside the OnBindViewHolder Method 从内部适配器调用notifyDataSetChanged失败 - Calling notifyDataSetChanged from inside adapter fails 调用notifyDataSetChanged - Calling notifyDataSetChanged NestedScrollView onBindViewHolder中的RecyclerView调用所有getItemCount大小 - RecyclerView inside NestedScrollView onBindViewHolder calling for all getItemCount size Scrollview内部的Recyclerview未调用自定义适配器函数(onBindViewHolder) - Recyclerview inside a Scrollview not calling the Custom Adapter Function (onBindViewHolder) 如何修改notifyDataSetChanged以避免onBindViewHolder被调用 - How to modify notifyDataSetChanged to avoid onBindViewHolder being called RecyclerView Adapter notifyDataSetChanged()之后未调用onBindViewHolder - onBindViewHolder not called after RecyclerView Adapter notifyDataSetChanged() 每次调用 notifyDataSetChanged() 时,都会调用 onBindViewHolder() - Every time notifyDataSetChanged() is called, onBindViewHolder() is called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM