首页 > 解决方案 > 加载更多卡在加载视图 - 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更新适配器时会出现此问题

标签: androidandroid-recyclerview

解决方案


推荐阅读