Reputation: 1557
I have a ListView and I put in the following code to see the getView() process of my BaseAdapter.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d("getView()","Fetching Row: " + position);
...
}
As I scroll down my list slowly, I notice something strange happening. Even when I'm at list position 40-50ish, it's still calling getView() with a position of 0-7. Here is what my log looks like:
05-14 11:46:13.989: I/getView()(18681): Fetching Row: 45
05-14 11:46:14.039: I/getView()(18681): Fetching Row: 0
05-14 11:46:14.049: I/getView()(18681): Fetching Row: 1
05-14 11:46:14.049: I/getView()(18681): Fetching Row: 2
05-14 11:46:14.049: I/getView()(18681): Fetching Row: 3
05-14 11:46:14.059: I/getView()(18681): Fetching Row: 4
05-14 11:46:14.059: I/getView()(18681): Fetching Row: 5
05-14 11:46:14.059: I/getView()(18681): Fetching Row: 6
05-14 11:46:14.069: I/getView()(18681): Fetching Row: 7
05-14 11:46:14.320: I/getView()(18681): Fetching Row: 46
05-14 11:46:14.430: I/getView()(18681): Fetching Row: 0
05-14 11:46:14.430: I/getView()(18681): Fetching Row: 1
05-14 11:46:14.430: I/getView()(18681): Fetching Row: 2
05-14 11:46:14.430: I/getView()(18681): Fetching Row: 3
05-14 11:46:14.430: I/getView()(18681): Fetching Row: 4
05-14 11:46:14.430: I/getView()(18681): Fetching Row: 5
05-14 11:46:14.430: I/getView()(18681): Fetching Row: 6
05-14 11:46:14.430: I/getView()(18681): Fetching Row: 7
As you can see, in between row 45 and row 46, it called getView() for row 0-7, each time. This happens for every list item. I'm afraid this is causing performance issues.
Why is this happening? And is this normal behavior?
Upvotes: 0
Views: 2475
Reputation: 18151
In your listview xml change the layout_height to android:layout_height="match_parent"
Upvotes: 5
Reputation: 7663
This is normal behavior. The ListView
has a number of internal optimizations to actually improve performance - one of which is to recycle views and only display rows that are actually on the screen. In other words, ListView
does not work by rendering all x number of views in your adapter and then simply move them on and off the screen. Rather, it creates views as they are displayed. This is why when you're on item 45 in your list it still refers to those rows as item 0 through 7. The ListView
is only displaying 7 or 8 views at a time - i.e. what fits on the screen - even though your adapter contains many more. This makes a big difference as your lists get longer since it saves the device from having to render every view in advance.
As a result to get one of the views is slightly more complicated than simply using getView()
since there is a good chance, depending on the length of the list, that it may not exist.
Upvotes: 1