android - 加载更多卡在加载视图 - RecyclerView
问题描述
我正在尝试从这里在回收站视图中实现加载更多的逻辑
它适用于我显示/隐藏加载指示器,但是,当最后一次加载时,更多列表为空(大小 = 0)它不起作用并停留在加载,因为notifyItemRangeInserted
大小为零时不起作用。
所以有人可以帮我吗?
适配器代码:
public class NotificationAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final Context context;
private List<NotificationModel> notifications;
private final int VIEW_TYPE_ITEM = 0;
private final int VIEW_TYPE_LOADING = 1;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public NotificationAdapter(Context context, List<NotificationModel> notifications) {
this.context = context;
this.notifications = notifications;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
if (viewType == VIEW_TYPE_ITEM) {
View view = LayoutInflater.from(context).inflate(R.layout.recycler_notification, viewGroup, false);
return new CustomViewHolder(view);
} else {
View view = LayoutInflater.from(context).inflate(R.layout.item_loading, viewGroup, false);
return new LoadingViewHolder(view);
}
}
@Override
public int getItemViewType(int position) {
return notifications.size() - 1 == position ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
if (viewHolder instanceof CustomViewHolder) {
populateItemRows((CustomViewHolder) viewHolder, position);
} else if (viewHolder instanceof LoadingViewHolder) {
showLoadingView((LoadingViewHolder) viewHolder, position);
}
}
@Override
public int getItemCount() {
return notifications == null ? 0 : notifications.size();
}
public class CustomViewHolder extends RecyclerView.ViewHolder {
TextView notification_time, notification_body;
public CustomViewHolder(@NonNull View itemView) {
super(itemView);
notification_time = itemView.findViewById(R.id.notification_time);
notification_body = itemView.findViewById(R.id.notification_body);
}
}
private void showLoadingView(LoadingViewHolder viewHolder, int position) {
//ProgressBar would be displayed
viewHolder.progressBar.setVisibility(View.VISIBLE);
}
private void populateItemRows(CustomViewHolder holder, int position) {
// row logic here
}
private class LoadingViewHolder extends RecyclerView.ViewHolder {
AVLoadingIndicatorView progressBar;
public LoadingViewHolder(@NonNull View itemView) {
super(itemView);
progressBar = itemView.findViewById(R.id.progressBar);
}
}
}
使用新数据刷新适配器的逻辑:
// notificationsList is the adapter data source where i append new data
if (notifications != null) {
int currentSize = notificationsList.size();
notificationsList.addAll(notifications);
notificationAdapter.notifyItemRangeInserted(currentSize, notifications.size());
}
使用notifyItemRangeInserted
和新数据大小 = 0更新适配器时会出现此问题
解决方案
推荐阅读
- rxjs6 - 共享运算符是否正常工作?
- php - 我使用带有 'while' 的 mysqli_fetch_assoc 创建了代码,但它没有工作
- laravel - 'date_subscribed' 在运行插入 SQL 时没有默认错误
- python - 如何为关键字创建adwords首页出价?
- h2o - H2O DAI 的 MLI 显示菜单是否依赖于其实验中使用的算法?
- powershell - Powershell脚本在Windows 10但不是7?
- java - 加载 POJO 父项时初始化 Java OOP 瞬态字段
- ios - UITableViewCell 滚动后被删除
- export - 从 Codepen 导出的笔不再有效。我错过了什么?
- python - 如何告诉 mypy 包含存根文件