1

The problem is based upon a specific use case, which is as follows: 1) Go the end of list and load more data until the API returns an empty list 2) After that call refresh() and the layout will refresh but the issue is that after I call refresh() the loadmore() doesn't gets called.

I have used SuperListView, I have used EndlessScrollListener with official SwipeToRefreshLayout, but nothing works.

I might be using the adapter in a wrong way I guess but below are the code snippets which I am using:

void showContent(){
  contentListModel = ((ContentListModel) baseModel);
  contentAdapter =  ContentAdapter(getActivity(),R.layout.content_item,contentListModel.getContents());
  mListView.setAdapter(contentAdapter);
}

void showRefreshContent(){
  contentListModel = new ContentListModel();
  contentAdapter.clear();
  contentListModel = ((ContentListModel) baseModel);
  contentAdapter.addAll(contentListModel.getContents());
  mListView.setAdapter(contentAdapter);
}

void showLoadMoreContent(){
  contentListModel = ((ContentListModel) baseModel);
  contentAdapter.addAll(contentListModel.getContents());
  contentAdapter.notifyDataSetChanged();
}

I could share the complete code but that might be irrelevant I guess, for backend I am using Spring + Android Annotations + [Observer + Facade] Design Patterns.

Thanks in advance.

EndlessScrollListener

public abstract class EndlessScrollListener implements AbsListView.OnScrollListener {
// The minimum amount of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 8;
// The current offset index of data you have loaded
private int currentPage = 1;
// The total number of items in the dataset after the last load
private int previousTotalItemCount = 0;
// True if we are still waiting for the last set of data to load.
private boolean loading = true;
// Sets the starting page index
private int startingPageIndex = 0;

public EndlessScrollListener() {
}

public EndlessScrollListener(int visibleThreshold) {
    this.visibleThreshold = visibleThreshold;
}

public EndlessScrollListener(int visibleThreshold, int startPage) {
    this.visibleThreshold = visibleThreshold;
    this.startingPageIndex = startPage;
    this.currentPage = startPage;
}

// This happens many times a second during a scroll, so be wary of the code you place here.
// We are given a few useful parameters to help us work out if we need to load some more data,
// but first we check if we are waiting for the previous load to finish.
@Override
public void onScroll(AbsListView view,int firstVisibleItem,int visibleItemCount,int totalItemCount)
{
    // If the total item count is zero and the previous isn't, assume the
    // list is invalidated and should be reset back to initial state
    if (totalItemCount < previousTotalItemCount) {
        this.currentPage = this.startingPageIndex;
        this.previousTotalItemCount = totalItemCount;
        if (totalItemCount == 0) { this.loading = true; }
    }
    // If it’s still loading, we check to see if the dataset count has
    // changed, if so we conclude it has finished loading and update the current page
    // number and total item count.
    if (loading && (totalItemCount > previousTotalItemCount)) {
        loading = false;
        previousTotalItemCount = totalItemCount;
        currentPage++;
    }

    // If it isn’t currently loading, we check to see if we have breached
    // the visibleThreshold and need to reload more data.
    // If we do need to reload some more data, we execute onLoadMore to fetch the data.
    if (!loading && (totalItemCount - visibleItemCount)<=(firstVisibleItem + visibleThreshold)) {
        onLoadMore(currentPage + 1, totalItemCount);
        loading = true;
    }
}

// Defines the process for actually loading more data based on page
public abstract void onLoadMore(int page, int totalItemsCount);

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    // Don't take any action on changed
}
}
Ahmad Ali Nasir
  • 1,372
  • 3
  • 16
  • 29
  • I used this with recyclerview. I also have the same problem. I found the problem. As you can see previousTotalItemCount set to 0 first. But when you refresh the list view it takes previous value. Ex. If four items are visible and if you refresh with SwipeRefresh, the value of previousTotalItemCount would be 4 but it should be 0. So solution is reset it's value when you first refresh with SwipeRefresh. – Rockers Technology Sep 21 '16 at 12:30

1 Answers1

0

You can Override listView methods, like in this snippet:

 @Override
    public void onScrollStateChanged(AbsListView absListView, int i)
    {
    }

    @Override
    public void onScroll(AbsListView absListView, int firstVisible, int visibleCount, int totalCount)
    {
        boolean loadMore = totalCount != totalLoadedCount && firstVisible + visibleCount >= totalCount;
        if (loadMore)
        {
            totalLoadedCount = totalCount;
            getContent(dto.loadedCount);
        }
    }

Where's in line totalCount != totalLoadedCount && firstVisible + visibleCount >= totalCount; you are detecting, if listView has been scrolled to the bottom, and getContent(dto.loadedCount); is method, that load new data to your list.

The full tutorial is here.

VadymVL
  • 4,561
  • 3
  • 24
  • 39
  • Oh, now I see. From a first look, there are some logical confusion: you are at first check `if (totalItemCount < previousTotalItemCount) {` but the next block `if (loading && (totalItemCount > previousTotalItemCount)) {`, that cancels previous block result. Maybe I'm wrong, but I think that the problem is hiding in this logic variable. – VadymVL Mar 19 '15 at 07:37
  • please check this reference, and its good https://github.com/codepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews – Ahmad Ali Nasir Mar 19 '15 at 07:41
  • 1
    the LoadMore works fine, the problem is the when I reach the end and there is no more data, I call refresh but after that the listView doesn't listens to the call I make to the onScrollListener ... – Ahmad Ali Nasir Mar 19 '15 at 07:44
  • Try to check this variable (`loading`), when this happens (for example, show it on Log). Maybe it resets, when there are no more data, and block other logic. – VadymVL Mar 19 '15 at 08:02