繁体   English   中英

如何更新 recyclerview 适配器中的特定项目范围

[英]How to update specific item range in recyclerview adapter

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

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

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

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

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

适配器 Class

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 Class (这是我的POJO class,仅供参考)

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是适配器 object。

使用此方法,您也不必调用notifydatachanged()或类似的方法。 这个链接对我帮助很大。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM