Problem with getListView().getFirstVisiblePosition()
I'd like to have my application do some on-screen changes (outside the ListView) based on what the first visible item in my list is.
What I've done is override the onScroll handler. When this handler is invoked, I set the position of my Cursor to the first visible position so I can look开发者_运维技巧 at the data for that row:
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
mCursor.moveToPosition(getListView().getFirstVisiblePosition());
...
I then reference the data in the Cursor to make my display changes.
Here's the thing: This working is spotty. Most of the time it works perfectly. I can't make it screw up.
However, sometimes getListView().getFirstVisiblePosition() does not return 1 until the 3rd item (index 2) is really the first visible item. It displays 0 when the 0 index is showing. It displays 0 when the 1 index is showing. It displays 1 when the 2 index is showing and so on. Additionally, it only does this when the user is scrolling down the list. If the user is scrolling backwards (up), getFirstVisiblePosition() returns the correct value.
I'm wondering if this isn't related to some efficiency problem. This handler is going to execute over and over and over as the user scrolls.
Does anyone have any ideas on what could be the problem? Does anyone have any better solutions?
Update:
Do I remember hearing that getFirstVisiblePosition() is really only a guess; and that Android essentially supposes what index might be available by rows' probable height? If this is so, then I have a problem. Some rows in my list are bigger than others. If this is the case, is there any way around it?
I would actually perform your call in onScrollStateChanged and only execute when scrolling is idle. You will drastically cut down on number of executed requests. Something like this:
public void onScrollStateChanged(final AbsListView view, final int scrollState) {
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
// do yer stuff
}
}
精彩评论