ExpandableHeightListView上的Inifinity Scroll无法正常工作

时间:2016-08-07 04:51:55

标签: java android listview expandablelistview infinite-scroll

我现在试图实施这几天,但到目前为止,没有成功。

InfiniteScrollListener.java

import android.widget.AbsListView;

public abstract class InfiniteScrollListener implements   AbsListView.OnScrollListener {
// The minimum number of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 2;
// The current offset index of data you have loaded
private int currentPage = 0;
// 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 InfiniteScrollListener() {
}

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

public InfiniteScrollListener(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 && (firstVisibleItem + visibleItemCount + visibleThreshold) >= totalItemCount ) {
        loading = onLoadMore(currentPage + 1, totalItemCount);
    }
}

// Defines the process for actually loading more data based on page
// Returns true if more data is being loaded; returns false if there is no more data to load.
public abstract boolean onLoadMore(int page, int totalItemsCount);

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    // Don't take any action on changed
}

}

然后在我的 HomeFragment.java

 @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.news, container,false);

    listview = (ExpandableHeightListView)v.findViewById(R.id.listview);

    FetchData();//this will fetch the first 10 data to populate the listview

    // Attach the listener to the AdapterView onCreate
    listview.setOnScrollListener(new InfiniteScrollListener() {
        @Override
        public boolean onLoadMore(int page, int totalItemsCount) {
            // Triggered only when new data needs to be appended to the list
            // Add whatever code is needed to append new items to your AdapterView
            customLoadMoreDataFromApi(page);//This line fetch more data
            // or customLoadMoreDataFromApi(totalItemsCount);

            return true; // ONLY if more data is actually being loaded; false otherwise.
        }
    });
    return v;
}


// Append more data into the adapter
public void customLoadMoreDataFromApi(int offset) {
        //code to fetch more data
}

我遇到的问题是,当listview首次填充10个项目时,它会自动调用customLoadMoreDataFromApi函数来加载更多项目而不用滚动,然后当我向下滚动到最后一个项目时,{即使从服务器加载更多项目,也永远不会再次调用{1}}。

我该如何解决这个问题?

0 个答案:

没有答案