如何更新回收视图适配器中的特定项目范围?

问题描述 投票:0回答:1

我有一个 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个回答
1
投票

你可以使用 DiffUtils 随着 AsyncListDiffer 来完成工作(我告诉你这些,但我并没有正确理解它的内部工作原理)。它使绘制工作变得简单,并由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();    }
}

应用信息类 (这是我刚才给出的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观察者中更改这一行。

recycler.getAdapter().notifyDataSetChanged(); 改为 appDrawerRVAdapter.submitList(appInfos); 哪儿 appDrawerRVAdapter 是适配器对象。

有了这个方法,你就不必再调用 notifydatachanged() 或类似的东西了。这个Link帮了我大忙。

© www.soinside.com 2019 - 2024. All rights reserved.