Push to refresh on android as in gmail (and not pull to refresh like on iphone)
Pergunta
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 ?
Solução
here is two helpful libraries:
Outras dicas
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;
}