Ccna final exam - java, php, javascript, ios, cshap all in one. This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Sunday, June 10, 2012
Android Endless List
How can I create a list where when you reach the end of the list I am notified so I can load more items?
One solution is to implement an OnScrollListener and make changes (like adding items, etc.) to the ListAdapter at a convenient state in its onScroll method.
The following ListActivity shows a list of integers, starting with 40, adding items when the user scrolls to the end of the list.
public class Test extends ListActivity implements OnScrollListener {
You should obviously use separate threads for long running actions (like loading web-data) and might want to indicate progress in the last list item (like the market or gmail apps do).
You can detect end of the list with help of onScrollListener, working code is presented below:
@Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (view.getAdapter() != null && ((firstVisibleItem + visibleItemCount) >= totalItemCount) && totalItemCount != mPrevTotalItemCount) { Log.v(TAG, "onListEnd, extending list"); mPrevTotalItemCount = totalItemCount; mAdapter.addMoreData(); } }
Another way to do that (inside adapter) is as following:
public View getView(int pos, View v, ViewGroup p) { if(pos==getCount()-1){ addMoreData(); //should be asynctask or thread } return view; }
Be aware that this method will be called many times, so you need to add another condition to block multiple calls of addMoreData().
When you add all elements to the list, please call notifyDataSetChanged() inside yours adapter to update the View (it should be run on UI thread - runOnUiThread)
Look at this article please. It has ready to use solution with custom adapter. http://codinglines.frankiv.me/post/14552677846/android-implementing-a-dynamically-loading-adapter
One solution is to implement an OnScrollListener and make changes (like adding items, etc.) to the ListAdapter at a convenient state in its onScroll method.
ReplyDeleteThe following ListActivity shows a list of integers, starting with 40, adding items when the user scrolls to the end of the list.
public class Test extends ListActivity implements OnScrollListener {
Aleph0 adapter = new Aleph0();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(adapter);
getListView().setOnScrollListener(this);
}
public void onScroll(AbsListView view,
int firstVisible, int visibleCount, int totalCount) {
boolean loadMore = /* maybe add a padding */
firstVisible + visibleCount >= totalCount;
if(loadMore) {
adapter.count += visibleCount; // or any other amount
adapter.notifyDataSetChanged();
}
}
public void onScrollStateChanged(AbsListView v, int s) { }
class Aleph0 extends BaseAdapter {
int count = 40; /* starting amount */
public int getCount() { return count; }
public Object getItem(int pos) { return pos; }
public long getItemId(int pos) { return pos; }
public View getView(int pos, View v, ViewGroup p) {
TextView view = new TextView(Test.this);
view.setText("entry " + pos);
return view;
}
}
}
You should obviously use separate threads for long running actions (like loading web-data) and might want to indicate progress in the last list item (like the market or gmail apps do).
I recommend CommonWare's EndlessAdapter: http://github.com/commonsguy/cwac-endless.
ReplyDeleteI found it from this answer to another question: Android: Implementing Endless List like Android Market.
You can detect end of the list with help of onScrollListener, working code is presented below:
ReplyDelete@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (view.getAdapter() != null && ((firstVisibleItem + visibleItemCount) >= totalItemCount) && totalItemCount != mPrevTotalItemCount) {
Log.v(TAG, "onListEnd, extending list");
mPrevTotalItemCount = totalItemCount;
mAdapter.addMoreData();
}
}
Another way to do that (inside adapter) is as following:
public View getView(int pos, View v, ViewGroup p) {
if(pos==getCount()-1){
addMoreData(); //should be asynctask or thread
}
return view;
}
Be aware that this method will be called many times, so you need to add another condition to block multiple calls of addMoreData().
When you add all elements to the list, please call notifyDataSetChanged() inside yours adapter to update the View (it should be run on UI thread - runOnUiThread)
Doesn't a ListAdaptor do exactly what you want? It represents a virtual list of items that only need to be loaded in as they are needed.
ReplyDeleteLook at this article please. It has ready to use solution with custom adapter. http://codinglines.frankiv.me/post/14552677846/android-implementing-a-dynamically-loading-adapter
ReplyDelete