2

There have been many requests for a push to refresh widget or library on android, even if some people consider it's not such a good ui pattern (I must say I belong to this camp).

But what about "pull to refresh" or to be more precise : the mechanism used in gmail for android 4. When you reach the bottom of the list, you get the last element. But if you scroll up again (push) then a new list item appears with a infinite progress bar and the next mails will load and fill up the list.

Other android coders and UI designers have noticed that, but I can't find any good debate about this pattern, neither can I find an implementation of this feature to fill a list.

Has anyone seen an interesting solution to this problem ?

Community
  • 1
  • 1
Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • Isn't that something like auto refreshing then? Like when you automatically prepend new items to the top of the list. And the problem here is that it doesn't work if you have changing content anywhere inside your list. – zapl Apr 24 '12 at 21:13

2 Answers2

3

here is two helpful libraries:

  • @Mohammed khalloufi, thx Mohammed but I am looking for a *push* to refresh library, not a *pull* to refresh one. – Snicolas Oct 15 '12 at 21:48
1

you can try implementing it by yourself. Its quite easy:

How I did:

1) set your listview's onScrollStateChange:

@Override
public void onScrollStateChanged(AbsListView listView, int scrollState) {
    if (scrollState == SCROLL_STATE_IDLE) {
        if (listView.getLastVisiblePosition() >= listView.getCount()-1) {
                //scroll reached the end, trigger the method to load more itens!                
        }
    }
}

2) in your base adapter:

private static final int TYPE_VIEW_COUNT = 2;
private static final int TYPE_VIEW_CONTENT = 0;
private static final int TYPE_VIEW_LOADING = 1; 

@Override
public int getItemViewType(int position) {

    if (position == (getCount()-1)) {
        return TYPE_VIEW_LOADING;
    } else {
        return TYPE_VIEW_CONTENT;
    }           

}

@Override
public int getViewTypeCount() {
    return TYPE_VIEW_COUNT;
}



@Override
public int getCount() {
    //return list size + 1;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View view = null;

    switch (getItemViewType(position)) {

    case TYPE_VIEW_CONTENT:

        //load your content view (using convertView and then...)

        break;
    case TYPE_VIEW_LOADING:
        //load your loading view (indeterminate progressbar)
        break;
    }



    return view;        

}
mrlew
  • 7,078
  • 3
  • 25
  • 28
  • Good answer! I would just add that it would be good to have a flag in the adapter that we can use to stop showing the "loading view" once we don't get any new data. We can use it in getItemViewType() like so: if (position == (getCount() - 1) && _shouldShowProgress_) {...} – Bandreid Jan 29 '16 at 15:29
  • For showing / hiding progress spinner best would be to use the ListView methods: _addFooterView(View view)_ and _removeFooterView(View view)_ – Bandreid Jan 29 '16 at 18:57