RecyclerView onBindViewHolder is called too many t

2020-07-22 09:48发布

问题:

I am using RecyclerView with GridLayoutManager. For each item in the grid, I need to call a REST api to retrieve data. Then, after get data asynchronously from remote,I use UIL to load/display image.

Everything seems to be fine. But i find onBindViewHolder is called too too too many times for specific items.

logcat log

Talk is cheap, let me show you the code:

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
    EditCourseImageAttachmentHolder holder = (EditCourseImageAttachmentHolder) viewHolder;
    holder.itemView.setLayoutParams(getLayoutParams(position));
    holder.attachmentView.initData(dataSource.get(headerView == null ? position : position - 1), urlMap);
}
public static class EditCourseImageAttachmentHolder extends RecyclerView.ViewHolder {

    public final AttachmentView attachmentView;

    public EditCourseImageAttachmentHolder(AttachmentView itemView) {
        super(itemView);
        this.attachmentView = itemView;
    }
}

-----------AttachmentView.Java-----------------

@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    subscription = tryLoadLocalImage(data)
            .subscribeOn(Schedulers.io())
            .flatMap(new Func1<String, Observable<String>>() {
                @Override
                public Observable<String> call(String localImageUrl) {
                    if (StringUtils.isNotBlank(localImageUrl)) {
                        return Observable.just(localImageUrl);
                    }
                    if (urlMap.containsKey(data.getUid())) {
                        return Observable.just(urlMap.get(data.getUid()));
                    } else {
                        //Remote API call
                        String qiNiuUrl = getQiNiuUrl(data);
                        urlMap.put(data.getUid(), qiNiuUrl);
                        return Observable.just(qiNiuUrl);
                    }
                }
            })
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Action1<String>() {
                @Override
                public void call(String localImageUrl) {
                    showImage(localImageUrl);
                }
            }, new Action1<Throwable>() {
                @Override
                public void call(Throwable throwable) {
                }
            });
}

@Override
protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    if (subscription != null && subscription.isUnsubscribed()) {
        subscription.unsubscribe();
    }
}

回答1:

Root cause is I changed the LayoutParam of the ItemView....

It works well after i remove those code. But, it's quite strange that only the item at specific position would re-bind.



回答2:

if (subscription != null && !subscription.isUnsubscribed()) {
    subscription.unsubscribe();
}

your code should be this...