【问题标题】:How to update specific item range in recyclerview adapter如何更新 recyclerview 适配器中的特定项目范围
【发布时间】:2020-05-04 16:27:28
【问题描述】:

我有一个recyclerView。我正在从服务器获取列表并将其显示在我的列表中。我有pagination,当我滚动到底部时,我请求server 获取下一个,例如50 项。唯一的问题是,当我收到响应时,我正在使用notifyDataSetChanged() 更新整个适配器。

mViewModel.getList().observe(
            getViewLifecycleOwner(),
            listResult -> {
                recycler.getAdapter().notifyDataSetChanged();
            });

这很简单,我使用的是ViewModel。当我的观察员通知时,我会致电notifyDataSetChanged()。我的适配器从ViewModel 获取列表。当我从服务器获取下 50 个项目时,我只需将新项目添加到现有列表中。在调用 notifyDataSetChanged() 之后,适配器会使用这 50 个新项目更新整个列表。那么如何只更新从服务器获得的最后 50 个项目呢?当我将此项目添加到列表中时,我需要通知适配器更新其数据。在那里我找到了一个notifyItemInserted();,但它需要位置。

【问题讨论】:

标签: java android android-recyclerview adapter notifydatasetchanged


【解决方案1】:

你可以使用 DiffUtilsAsyncListDiffer 来完成这项工作(我告诉你这个,但我没有正确理解它在内部是如何工作的)。它使绘图变得容易并由Android系统处理。我已阅读并发现 AsyncListDiffer 检查新项目并仅绘制新项目的一些帖子。

这里有一些代码可以帮助你:

适配器类

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.AsyncListDiffer;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.SortedSet;

import neilsayok.github.launchertest5.R;
import neilsayok.github.launchertest5.adapter.viewholder.AppDrawerItemVH;
import neilsayok.github.launchertest5.data.AppInfo;

public class AppDrawerRVAdapter extends RecyclerView.Adapter<AppDrawerItemVH> {


    private Context context;

    private AsyncListDiffer<AppInfo> appList;


    public AppDrawerRVAdapter(Context context) {
        this.context = context;
        appList = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<AppInfo>() {

            @Override
            public boolean areItemsTheSame(@NonNull AppInfo oldItem, @NonNull AppInfo newItem) {
                return TextUtils.equals(oldItem.getPackageName(), newItem.getPackageName());
            }

            @Override
            public boolean areContentsTheSame(@NonNull AppInfo oldItem, @NonNull AppInfo newItem) {
                return TextUtils.equals(newItem.getLable(), oldItem.getLable());
            }
        });
    }

    public void submitList(SortedSet<AppInfo> data) {
        appList.submitList(new ArrayList<AppInfo>(data));
    }


    @NonNull
    @Override
    public AppDrawerItemVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AppDrawerItemVH(LayoutInflater.from(context).inflate(R.layout.app_drawer_item,
                parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull AppDrawerItemVH holder, int position) {
        final AppInfo app = appList.getCurrentList().get(position);
        holder.getAppIcon().setImageDrawable(app.getAppIcon());
        holder.getAppLable().setText(app.getLable());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(app.getPackageName());
                context.startActivity(launchIntent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (appList == null)
            return 0;
        else
            return appList.getCurrentList().size();    }
}

AppInfo 类 (这是我的 POJO 类,仅供参考)

import android.graphics.drawable.Drawable;

public class AppInfo {
    private String lable;
    private String packageName;
    private Drawable appIcon;

    public AppInfo() {
    }

    public AppInfo(String lable, String packageName, Drawable appIcon) {
        this.lable = lable;
        this.packageName = packageName;
        this.appIcon = appIcon;
    }

    public String getLable() {
        return lable;
    }

    public void setLable(String lable) {
        this.lable = lable;
    }

    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    public Drawable getAppIcon() {
        return appIcon;
    }

    public void setAppIcon(Drawable appIcon) {
        this.appIcon = appIcon;
    }
}


在您的 ViewModel Observer 中更改此行:

recycler.getAdapter().notifyDataSetChanged();appDrawerRVAdapter.submitList(appInfos); 其中 appDrawerRVAdapter 是适配器对象。

使用这种方法,您将不必调用notifydatachanged() 或类似的东西。This Link helped me a lot.

【讨论】:

    猜你喜欢
    • 2015-12-04
    • 2021-01-24
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 2015-09-30
    • 2020-07-27
    • 2018-11-23
    相关资源
    最近更新 更多