-1

I have RecyclerView.Adapter<RecyclerView.ViewHolder> with pagination button(Load More)

Load More button in RecyclerView like Footer. Each request get 20 rows and if I get <20 items I need disable this button:

 @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ...
        } else if (holder instanceof FooterViewHolder) {
            ...
            viewHolder.loadMore.setEnabled(isVisibleLoadButton);
        }
    }

public void addData(List<TransactionItem> opcTransactions) {
        final int positionStart = transactionItems.size()+1;
        isVisibleLoadButton = opcTransactions.size() >= 20;
        transactionItems.addAll(opcTransactions);
        notifyItemRangeInserted(positionStart, opcTransactions.size());
    }

If I get opcTransactions with 1 or more items isVisibleLoadButton set false and my load More button set disable. But If I get emty list opcTransactions size = 0 - load More button not diasbled.

@Override
    public int getItemCount() {
        if (transactionItems == null) {
            return 0;
        }

        if (transactionItems.size() == 0) {
            //Return 1 here to show nothing
            return 1;
        }

        // Add extra view to show the footer view
        return transactionItems.size() + 1;
    }
ip696
  • 6,574
  • 12
  • 65
  • 128

1 Answers1

0

Problem is in this line .

final int positionStart = transactionItems.size()+1;

positionStart should point to last item of your list . and indexing starts form ZERO. Use it as .

final int positionStart = transactionItems.size();
ADM
  • 20,406
  • 11
  • 52
  • 83
  • 1
    not. I use this specially. if I add +1 - when I load first 20 items my list not scroled to buttom (https://stackoverflow.com/a/30455749/4781349) and if I not use +1 button not disabled too – ip696 Dec 22 '17 at 09:48
  • Whats the point of +1? when you are ultimately calling `notifyItemRangeInserted` not `notifyItemChanged` . – ADM Dec 22 '17 at 09:52
  • I do not understand you. I wrote a link with the answer why I use +1. and clarified that even if I do not use +1, it still does not work – ip696 Dec 22 '17 at 09:58