1

I have a requirement

I have a large data have to put in the list view and it's too stupid to load all data and populate in list view so i load 10 first item from server and populate in listView.

So everytime user scroll down at the bottom of listview ( They viewed all first 10 item ) my app will load the next 10 item automatically.

Problem is : Is there anyway that i can detect that whether user is at the bottom of the first 10 item or not ?

Sorry about my English . appreciate for any help !

erijack
  • 57
  • 1
  • 2
  • 9
  • See [this answer](http://stackoverflow.com/a/16399081/1537347) for an elegant solution to your problem. – Jay Oct 17 '13 at 16:48

4 Answers4

0

You can detect the end of scrolling by the following code

if (yourListView.getLastVisiblePosition() == yourListView.getAdapter().getCount() -1 &&
    yourListView.getChildAt(yourListView.getChildCount() - 1).getBottom() <= yourListView.getHeight())
{
    //It is scrolled all the way down here

}

Hope it helps.

ayon
  • 2,180
  • 2
  • 17
  • 32
0

You can register a listener to the OnScroll event and then detect where you have to start reloading data:

ListView listView = (ListView)findViewById(R.id.list_view_id);
listView.setOnScrollListener(new OnScrollListener() {
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int amountVisible, int totalItems) {
            //now get the point where you have to reload data
            if (firstVisibleItem+1 + amountVisible > totalItems) {
                //reload your data here
            }
        }
    });
productioncoder
  • 4,225
  • 2
  • 39
  • 65
0

Where's the data coming from ? It's common to load large data sets from a DB via a cursor, and then use a simple cursor adapter to populate the list view. (And I'm talking 10,000s of rows). So long as you do it in the background (cursor loader) it shouldn't be a problem, and it probably a lot easier than trying to manage the scrolling yourself.

K5 User
  • 606
  • 1
  • 6
  • 10
0

Try this, it implements a listview that pulls more items when the user reaches the bottom, also using a progressbar in the bottom when is loading more items. I have used it before and it works well for what you need.

Mikel
  • 1,581
  • 17
  • 35
  • Thank you very much . I try to run the example , and it work perfectly but when I try to implement it into my project and when i execute the code , JavaClassCast Exeption is throw " Can not be cast androi.wiget.ListView to com.costum.android.widget.PullAndLoadListView" In my project i customize a adapter and populate it to PullAndLoadListView . It display perfectly but when execute this code ((PullAndLoadListView) getListView()) the exeption above appeared . Any idea ? Plz help me – erijack Oct 18 '13 at 15:24
  • 1
    Did you write your xml with com.costum.android.widget.PullAndLoadListView instead of ListView? – Mikel Oct 18 '13 at 15:26