你可以使用 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(); }
}
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.