我以前使用 RecyclerView Adapter 并决定切换到 ListAdapter 以使用 DiffUtils 进行动画。起初一切似乎都很顺利,直到我从任务切换器(内存)中清除了我的应用程序。一旦我再次打开我的应用程序,所有项目都消失了,这是使用 SQLite 数据库实现的。原来我必须向 RecyclerView 添加一个新项目才能显示以前的项目。这发生在我切换到 ListAdpater 之后,我认为这是罪魁祸首。任何帮助,将不胜感激。提前致谢。
我的适配器类
public class CourseAdapter extends ListAdapter<Course, CourseAdapter.CourseHolder> {
private OnItemClickListener listener;
public CourseAdapter() {
super(DIFF_CALLBACK);
}
private static final DiffUtil.ItemCallback<Course> DIFF_CALLBACK = new DiffUtil.ItemCallback<Course>() {
@Override
public boolean areItemsTheSame(@NonNull Course oldItem, @NonNull Course newItem) {
return oldItem.getId() == newItem.getId();
}
@Override
public boolean areContentsTheSame(@NonNull Course oldItem, @NonNull Course newItem) {
return oldItem.getName().equals(newItem.getName()) &&
oldItem.getBuilding().equals(newItem.getBuilding()) &&
oldItem.getRoom().equals(newItem.getRoom()) &&
oldItem.getDays().equals(newItem.getDays()) &&
oldItem.getStartTime().equals(newItem.getStartTime()) &&
oldItem.getEndTime().equals(newItem.getEndTime()) &&
oldItem.getProfName().equals(newItem.getProfName()) &&
oldItem.getEmail().equals(newItem.getEmail()) &&
oldItem.getProfBuilding().equals(newItem.getProfBuilding()) &&
oldItem.getProfRoom().equals(newItem.getProfRoom()) &&
oldItem.getProfDays().equals(newItem.getProfDays()) &&
oldItem.getProfStartTime().equals(newItem.getProfStartTime()) &&
oldItem.getProfEndTime().equals(newItem.getProfEndTime()) &&
oldItem.getColor() == newItem.getColor();
}
};
@NonNull
@Override
public CourseHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.course_item, parent, false);
return new CourseHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull CourseHolder holder, int position) {
Course currentCourse = getItem(position);
holder.card.setCardBackgroundColor(currentCourse.getColor());
holder.textViewName.setText(currentCourse.getName());
holder.textViewDays.setText(currentCourse.getDays());
String startTime = currentCourse.getStartTime();
String endTime = currentCourse.getEndTime();
if (startTime.isEmpty() && endTime.isEmpty()) {
holder.textViewTime.setText("");
} else {
holder.textViewTime.setText(startTime + "-" + endTime);
}
}
public Course getCourseAt(int position) {
return getItem(position);
}
class CourseHolder extends RecyclerView.ViewHolder {
private TextView textViewName;
private TextView textViewDays;
private TextView textViewTime;
private MaterialCardView card;
public CourseHolder(@NonNull View itemView) {
super(itemView);
textViewName = itemView.findViewById(R.id.text_view_name);
textViewDays = itemView.findViewById(R.id.text_view_days);
textViewTime = itemView.findViewById(R.id.text_view_time);
card = itemView.findViewById(R.id.card_view_courses);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getAdapterPosition();
if (listener != null && position != RecyclerView.NO_POSITION) {
listener.onItemClick(getItem(position), position, textViewName);
}
}
});
}
}
public interface OnItemClickListener {
void onItemClick(Course course, int position, TextView title);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
}
在主要活动中
RecyclerView recyclerView = findViewById(R.id.recycler_view_courses);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
CourseAdapter adapter = new CourseAdapter();
recyclerView.setAdapter(adapter);
viewModel = ViewModelProviders.of(this).get(ViewModel.class);
viewModel.getAllCourses().observe(this, new Observer<List<Course>>() {
@Override
public void onChanged(List<Course> courses) {
adapter.submitList(courses);
}
});