Question

So I have a ListView and also up and down buttons.

My goal is to scroll the ListView by the it's height everytime I press one of the buttons, simple up / down navigation.

The scrolling does work, sort of. When I press down, the ListView scrolls by the correct distance every time it scrolls.

However, only the up button works as intended. The down button has problems which I believe are due to the ListView's first visible scroll position not updating.

When I press down, the onNavigationDownPressed() method occurs as seen below. This scrolls the correct amount down the ListView with a starting ListView firstVisiblePosition at 0. The next time that method is called, the first visible position is still 0.

But if I manually scroll the ListView down ever so slightly and then press down, it scrolls down correctly and then the same thing, it won't go down any more due to the firstVisiblePostion not changing unless I scroll first to make the position update.

// Scroll up
@Override
public void onNavigationUpPressed()
{       
    Log.i("MAKE UP, FIRST VISIBLE POSITION: ", String.valueOf(listView.getFirstVisiblePosition()));     
    listView.setSelectionFromTop(listView.getFirstVisiblePosition(), fragmentHeight);
}

// Scroll down
@Override
public void onNavigationDownPressed()
{
    Log.i("MAKE DOWN, FIRST VISIBLE POSITION: ", String.valueOf(listView.getFirstVisiblePosition()));       
    listView.setSelectionFromTop(listView.getFirstVisiblePosition(), -fragmentHeight);
}   

Any ideas?

Cheers

Was it helpful?

Solution 2

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.

OTHER TIPS

Perhaps you should try to call :

listView.smoothScrollToPosition(yourPosition);

Another route is to manually set the scroll of the items but the first approach should work for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top