How can I create a list where when you reach the end of the list I am notified so I can load more items?
At Ognyan Bankov GitHub i found a simple and working solution!
It makes use of the Volley HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available through the open AOSP repository.
The given code demonstrates:
For future consistence i forked Bankov's repo.
May be a little late but the following solution happened very useful in my case.
In a way all you need to do is add to your ListView a Footer
and create for it addOnLayoutChangeListener
.
http://developer.android.com/reference/android/widget/ListView.html#addFooterView(android.view.View)
For example:
ListView listView1 = (ListView) v.findViewById(R.id.dialogsList); // Your listView
View loadMoreView = getActivity().getLayoutInflater().inflate(R.layout.list_load_more, null); // Getting your layout of FooterView, which will always be at the bottom of your listview. E.g. you may place on it the ProgressBar or leave it empty-layout.
listView1.addFooterView(loadMoreView); // Adding your View to your listview
...
loadMoreView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
Log.d("Hey!", "Your list has reached bottom");
}
});
This event fires once when a footer becomes visible and works like a charm.
The key of this problem is to detect the load-more event, start an async request for data and then update the list. Also an adapter with loading indicator and other decorators is needed. In fact, the problem is very complicated in some corner cases. Just a OnScrollListener
implementation is not enough, because sometimes the items do not fill the screen.
I have written a personal package which support endless list for RecyclerView
, and also provide a async loader implementation AutoPagerFragment
which makes it very easy to get data from a multi-page source. It can load any page you want into a RecyclerView
on a custom event, not only the next page.
Here is the address: https://github.com/SphiaTower/AutoPagerRecyclerManager
I've been working in another solution very similar to that, but, I am using a footerView
to give the possibility to the user download more elements clicking the footerView
, I am using a "menu" which is shown above the ListView
and in the bottom of the parent view, this "menu" hides the bottom of the ListView
, so, when the listView
is scrolling the menu disappear and when scroll state is idle, the menu appear again, but when the user scrolls to the end of the listView
, I "ask" to know if the footerView
is shown in that case, the menu doesn't appear and the user can see the footerView
to load more content. Here the code:
Regards.
listView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
if(scrollState == SCROLL_STATE_IDLE) {
if(footerView.isShown()) {
bottomView.setVisibility(LinearLayout.INVISIBLE);
} else {
bottomView.setVisibility(LinearLayout.VISIBLE);
} else {
bottomView.setVisibility(LinearLayout.INVISIBLE);
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
});