Initially the listView has 20 items. I need to update the listView with next 20 items when user scrolls down all the way to the bottom. I am using ArrayAdapter to achieve this. The items in the listView are getting from the REST API which takes startIndex and the limit as parameter using EventBus(not AsyncTask).
public void onEventMainThread(MessageListEvent event) {
//Create Adapter
//Set Adapter to List View
customListAdapter = new CustomListAdapter(getActivity(), event.itemList);
Log.d("Scroll","Inside OnEventMainThread");
mListView = (ListView) view.findViewById(android.R.id.list);
mListView.setAdapter(customListAdapter);
mListView.setOnItemClickListener(this);
// This loads the next 20 images when scrolled all the way down to bottom.
// Overrides onScroll and onScrollStateChanged function
mListView.setOnScrollListener(this);
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
int loaded = firstVisibleItem+visibleItemCount;
if(loaded>=totalItemCount && startIndex+19==loaded) {
startIndex = loaded+1; //Static variable used in onEventBackgroundThread(...). Download starts from this item
EventBus.getDefault().post(new StartDownloadEvent()); // This will download the next 20 and update the list to 40 items but position starts from 0 again. onEventBackgroundThread(...) downloads the data and updates the list.
}
}
I found similar problem in stackOverflow but all are using AsyncTask. I don't want to use AsyncTask and would like to achieve using EventBus.