Solution:
I set an onScrollListener for the ListView which notified me both when the ListView had scrolled and some other handy parameters such as first visible item index, total number of items visible on screen etc. so I stored these variables.
listView.setOnScrollListener(new OnScrollListener()
{
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
{
Log.i("MMY: ", "Scroll");
Log.i("MMY: ", "First visible item: " + String.valueOf(firstVisibleItem));
Log.i("MMY: ", "Visible item count: " + String.valueOf(visibleItemCount));
// The variables that belong to this class now equal ->
General_Navigation_Fragment.this.visibleItemCount = visibleItemCount;
General_Navigation_Fragment.this.firstVisibleItem = firstVisibleItem;
totalCount = totalItemCount;
}
});
When the down button was pressed, I got the index of the first visible item and I got the number of visible items (n) on the screen and then scrolled down to current position + n. Simply did the reverse for scrolling up.
// Scroll up
@Override
public void onNavigationUpPressed()
{
currentVisibleItem = firstVisibleItem - visibleItemCount;
if (currentVisibleItem < 0)
{
listView.setSelection(0);
}
else
listView.setSelection(currentVisibleItem - 1);
}
// Scroll down
@Override
public void onNavigationDownPressed()
{
currentVisibleItem = visibleItemCount + firstVisibleItem;
listView.setSelection(currentVisibleItem - 1);
}
Since I have varying size items in my ListView, this way seemed the easiest way to go about it.