编程语言
首页 > 编程语言> > RecyclerView 局部刷新的坑,34岁程序员年薪50w

RecyclerView 局部刷新的坑,34岁程序员年薪50w

作者:互联网

踩坑

而我们要做的是一个下载列表,在下载的callback里面我们要去更新列表的progressBar还有Button的文本:

Activity中获取到下载的callback:

@Override
public void onProgress(long finished, long total, int progress) {
XLog.e(mInfoBean.getDownLoadUrl() + “-----------||||||” + progress);
String downloadPerSize = Kits.File.getDownloadPerSize(finished, total);
mInfoBean.setDownLoadStatus(STATUS_DOWNLOADING);
mInfoBean.setDownloadPerSize(downloadPerSize);
mInfoBean.setProgress(progress);
if (isCurrentListViewItemVisible(mPosition)) {
mTestDownLoadAdapter.setProgress(progress, mPosition, downloadPerSize);
}
}

适配器中暴露的用于更新progressBar进度的方法

/**

适配器的onBindViewHolder()中处理数据结果

@Override
public void onBindViewHolder(final TestDownLoadHolder holder, final int position) {
final AppInfoBean appInfoBean = data.get(position);
if (appInfoBean != null) {
ILFactory.getLoader().loadNet(holder.mIvDownPic, appInfoBean.getDownLoadPic(), ImgOptions.getDefImaOptions());
holder.mPbDownProgress.setProgress(appInfoBean.getProgress());
holder.mBtDownLoad.setText(appInfoBean.getDownloadPerSize());
holder.mBtDownLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (getRecItemClick() != null) {
getRecItemClick().onItemClick(position, appInfoBean, 0, holder);
}
}
});
}

}

这里我们使用的是notifyItemChanged局部刷新而不是notifyDataSetChanged。讲道理我们到了这一步就已经可以满足需求了,然而…事实不讲道理:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-D02S7a5j-1646116102674)(https://user-gold-cdn.xitu.io/2017/10/27/72395e18c6486bb49278862724e630b4?imageView2/0/w/1280/h/960/ignore-error/1)]

一闪一闪亮晶晶

额…原谅demo画得太丑了。这里我们可以发现…为毛我的图片…一闪一闪的,亮瞎我的狗眼了。
至于为什么会闪动?
猜测:

  1. ImageView的宽高问题,重新计算导致了闪动
  2. 更新到了ImageView,然后glide又重新加载了一次图片。
  3. 刷新到了ImageView,这个item渲染了一次。

解疑:

ILFactory.getLoader().loadNet(holder.mIvDownPic, appInfoBean.getDownLoadPic(), ImgOptions.getDefImaOptions());

android:src="@mipmap/ic_launcher"

那我们的问题就聚焦在如何只更新一部分控件,其他控件不动

解决

至于怎么解决呢…扒源码:

/**

/**

在这里我们看到一句话:

@param payload Optional parameter, use null to identify a “full” update

传入“null”就是更新全部!那我们调用的时候是传入什么呢!看下:

public final void notifyItemChanged(int position) {
mObservable.notifyItemRangeChanged(position, 1);
}

继而调用:

public void notifyItemRangeChanged(int positionStart, int itemCount) {
notifyItemRangeChanged(positionStart, itemCount, null);
}

继而调用:

public void notifyItemRangeChanged(int positionStart, int itemCount, Object payload) {
// since onItemRangeChanged() is implemented by the app, it could do anything, including
// removing itself from {@link mObservers} - and that could cause problems if
// an iterator is used on the ArrayList {@link mObservers}.
// to avoid such problems, just march thru the list in the reverse order.
for (int i = mObservers.size() - 1; i >= 0; i–) {
mObservers.get(i).onItemRangeChanged(positionStart, itemCount, payload);
}
}

[捂脸] 一个大写的null…

OK,那我们修改下我们的调用方法:

/**

/**

这里我们传入了一个 “1”,这个1是随意传入的,其他值也可以。

继而我们需要重写onBindViewHolder方法如下:

@Override
public void onBindViewHolder(TestDownLoadHolder holder, int position, List payloads) {
XLog.e("--------------------------has payloads");
if (payloads.isEmpty()) {
XLog.e("--------------------------no payloads");
onBindViewHolder(holder, position);
} else {
XLog.e("--------------------------false payloads");
final AppInfoBean appInfoBean = data.get(position);
if (appInfoBean != null) {
holder.mPbDownProgress.setProgress(appInfoBean.getProgress());
holder.mBtDownLoad.setText(appInfoBean.getDownloadPerSize());
}
}
}

@Override
public void onBindViewHolder(final TestDownLoadHolder holder, final int position) {
final AppInfoBean appInfoBean = data.get(position);
if (appInfoBean != null) {
ILFactory.getLoader().loadNet(holder.mIvDownPic, appInfoBean.getDownLoadPic(), ImgOptions.getDefImaOptions());
holder.mPbDownProgress.setProgress(appInfoBean.getProgress());
holder.mBtDownLoad.setText(appInfoBean.getDownloadPerSize());
holder.mBtDownLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (getRecItemClick() != null) {
getRecItemClick().onItemClick(position, appInfoBean, 0, holder);
}
}
});
}

}
getDownloadPerSize());
holder.mBtDownLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (getRecItemClick() != null) {
getRecItemClick().onItemClick(position, appInfoBean, 0, holder);
}
}
});
}

}

标签:int,holder,void,34,appInfoBean,position,50w,RecyclerView,public
来源: https://blog.csdn.net/m0_66685346/article/details/123205254