1

I know it's somehow possible to make a ListView that loads more data when the user has reached the bottom of the list. However, I'm working with a ScrollView, which I have a LinearLayout in, and these two components works great with the scrolling and so. But I don't know how I'm supposed to do so it gets an infinite scroll.

I suppose I need to add something that reads what is shown of the LinearLayout on the screen, and when it calculates that it is the bottom of the LinearLayout that is being shown (by using the current position and the height of the View), it triggers an event.
But as I said, I don't know how to accomplish this. So, if anyone can give me some help I would be very grateful.

EDIT: I found this post here on StackOverflow How to trigger an event when scrollView reach the bottom with Android?, but I don't know what to do with the answer:

Given the requirements, you'll likely be extending BaseAdapter (as opposed to CursorAdapter which utilizes a different mechanism).

Here's a snippet for that:

public View getView(int position, View convertView, ViewGroup parent) {
    if (position == backingLinkedList.size()) {
        //get more items and add them to the backingLinkedList in a background thread
        notifyDataSetChanged();
    }
}
Community
  • 1
  • 1
Daniel Jonsson
  • 3,261
  • 5
  • 45
  • 66

2 Answers2

0

I know this was solved a long time ago, but I wanted to share my solution for future infinite scrollers.

I used the onScroll method within the OnScrollListener to trigger a background thread to grab more data and adapter.notifyDataSetChanged to notify the ListView of more data to load and create an infinite scroll. For some reason, onScrollStateChanged wasn't triggering, but onScroll did what I needed so I didn't need to bother.

For the longest time I didn't understand how to keep the ListView from reloading and placing me back at the top of the list, but then I realized I only need to initialize a new adapter and call setListAdapter once, and every time after that, only call notifyDataSetChanged. Before, I was also calling setListAdapter, thereby placing me back at the top.

craned
  • 2,991
  • 2
  • 34
  • 38
0

If you can trigger an event when you reach the bottom of a scrollview, You could try creating your own type of view to hold your data. When you reach the bottom of the view load the next data into your view class and add it into the layout of the scroll view.

So basically create a class that holds a section of what your showing. Add the first one into the scrollview. When you hit the bottom create another view holding your next data and use scrollviews_layout.addview(view_holding_the_new_data)

Buneme Kyakilika
  • 1,202
  • 3
  • 13
  • 34
The Dude
  • 310
  • 7
  • 15