Pedro Paulo Amorim
Pedro Paulo Amorim

Reputation: 1949

ListView problems with footer(height)

I'm trying to improve my listview more loader, did not understand much how it works but I've got to do something. But what is happening in the first footer in listview I add, he is "damaged" in height as shown in this image:

The class code is here.

A snippet:

 @Override
 public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    try {
        if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0)
        {
            if(!mIsLoading) {
                if(mListView.getFooterViewsCount() >= 0) {
                    mListView.addFooterView(mFooterView);
                }
                new LoadMore().execute();
                mIsLoading = true;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

The bugged image is: screenshot 1 and the correct image is screenshot 2.

If any kind soul want to properly explain how this "load more" works, I thank.

Upvotes: 0

Views: 160

Answers (1)

jpros
jpros

Reputation: 323

Pedro,

I was doing once the same mistake as you. Don't control this kind of stuff in onScrollListner. The right way to load data, and check if more data is needed, is in the Adapter.

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    if (!loading && hasNext && i > getCount() - 10) {
        page++;
        new NewsAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, page);
    } 
    ... 
}

I also recommend you to put a loading view in ListView's footer, and only when there is no more data to load, you change his visibility to GONE.

Imagine if in one activity you need load 3 different data, it would be a completely mess. Doing this way will be far more productive and you will see a clean code!

Hope it helps.

Regards, Joao Paulo Ros

Upvotes: 1

Related Questions