0

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.

Aditya Vyas-Lakhan
  • 12,393
  • 15
  • 55
  • 92
Prit
  • 1
  • 1

3 Answers3

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

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 :)

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" />
Jayakrishnan
  • 3,637
  • 26
  • 25