Загрузить больше застрял в представлении загрузки - RecyclerView - PullRequest
0 голосов
/ 29 марта 2020

Я пытаюсь реализовать логику c загрузки больше в представлении переработчика из здесь

, это хорошо работает, когда я показываю / скрываю индикатор загрузки, но, когда последний загрузить больше список пуст (размер = 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);
        }
    }
}

logi c для refre sh адаптер с новыми данными:

               // 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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...