我有一个 RecyclerView
,其中包含一个 Product
列表,其中包含图像、文本和一个 CountDownTimer
,我为每个调用一个接口(interface)回调秒从我的 Product
类更改为我的 Adapter
。我只需要刷新布局的一个 TextView
,但 notifyItemChanged
调用 onBindViewHolder
并刷新我项目的所有数据。仅刷新项目的一个 TextView 的最佳方法是什么?
public void onBindViewHolder(final ViewHolderCustom holder, final int position) {
final Product currentProduct = listProduct.get(position);
holder.productBrand.setText(currentProduct.getBrand());
holder.productName.setText(currentProduct.getName());
holder.productPrice.setText(currentProduct.getPrice());
holder.time.setText(String.valueOf(currentProduct.getTime()));
String urlImg = currentProduct.getPicture();
Log.v("adapter", "item " + position);
if (!urlImg.equals("empty")) {
imageLoader.get(urlImg, new ImageLoader.ImageListener(){...});
}
if (currentProduct.getId() == 1 && test == 0) {
test = 1;
currentProduct.Start_countDown(new Product.TimeCallBack() {
@Override
public void timeCallback(Product product) {
int index = listProduct.indexOf(product);
notifyItemChanged(index);
}
});
}
}
最佳答案
notifyItemChanged(position);
是您想要重绘整个项目的正确方法。当背景有大图像时,即使缓存了图像,如果对同一项目调用 notifyItemChanged
过快,也会看到一些闪烁。
对于您的用例,您只需要重绘一个 View ,这样您就可以像这样直接更新它:
public void updateTime(ViewHolderCustom holder) {
holder.time.setText(String.valueOf(currentProduct.getTime()));
holder.time.invalidate();
}
关于android - 如何仅刷新 RecyclerView 中项目的 Textview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32178002/