android - 如何仅刷新 RecyclerView 中项目的 Textview

标签 android android-recyclerview countdowntimer

我有一个 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/

相关文章:

android - 在两个 fragment 之间添加一个监听器

java - 当 Activity 不是当前 Activity 时,如何告诉 MyCountDownTimer 退出?

android - 如何在特定时间内重复播放相同的声音?

android - CountDownTimer 更新和阻塞

android - 如何在android中将多个文件附加到电子邮件客户端

android - 为什么我的 this.setState 在函数中不起作用?

android - 如何在 Facebook Android SDK 中支持较早的 API 版本?

android - 我无法打开通知作为第一个通知

android - 在 Recyclerview 图像在滚动时放错了位置

android - Livedata正在执行一项 Activity ,但未进行另一项 Activity