I have a listview
and an array of say 30 items in it, now I want to show first 10 items only when I open it, and then as I scroll to bottom I want to show a load more button which when clicked will add next 10 items to the listview
and again on scrolling to bottom it should show load more button. I just don't know how to handle this, any help would be appreciated.
Asked
Active
Viewed 1,501 times
0
![](../../users/profiles/4407266.webp)
Aditya Vyas-Lakhan
- 12,393
- 15
- 55
- 92
![](../../users/profiles/6617079.webp)
Prit
- 1
- 1
-
see my answer and let me know – Aditya Vyas-Lakhan Jul 21 '16 at 05:13
-
http://stackoverflow.com/questions/17473933/android-listview-setonscrolllistener and http://stackoverflow.com/questions/31000964/how-to-implement-setonscrolllistener-in-recyclerview may help you – AndroidGeek Jul 21 '16 at 13:48
3 Answers
0
listv.setOnScrollListener(new OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0)
{
if(flag_loading == false)
{
flag_loading = true;
addmoreitems();
}
}
}
});
and then you can add next ten items in addmoreitems(). it will work.
If you want to add button at footer of listview just add yourlistview.addFooterView. Just try this code
![](../../users/profiles/4407266.webp)
Aditya Vyas-Lakhan
- 12,393
- 15
- 55
- 92
0
We can add Load More button to list view using listview.addFooterView(btnLoadMore)
then, we can ada a click event listener to load more button and call a background thread which will append more data to listview.
here is the complete tutorial on listview with load more button
hope this helps :)
![](../../users/profiles/6285410.webp)
Mr.7
- 1,937
- 1
- 17
- 29
0
Make following changes to ListAdapter class
@Override
public View getView(int position, View convertView, ViewGroup parent) {
....
if (mTotalPages > mData.size()) {
if ((position + 1) == getCount()) {
holder.sLoadingView.setVisibility(View.VISIBLE);
} else {
holder.sLoadingView.setVisibility(View.GONE);
}
}
....
}
Where mTotalPages = 30 in your case.
In adapter layout add load more view at the bottom like this,
<TextView
android:id="@+id/loading_more_items"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/content_wrapper"
android:layout_centerHorizontal="true"
android:layout_margin="@dimen/space"
android:clickable="false"
android:gravity="start"
android:singleLine="false"
android:text="Loading..."
android:textColor="@color/dark_grey"
android:textSize="@dimen/text_size_small"
android:visibility="gone" />
![](../../users/profiles/6021927.webp)
Jayakrishnan
- 3,637
- 26
- 25