user1460340
user1460340

Reputation:

How to start display a list-view from specific item position

I have a list view of 300 mp3 songs and suppose song 52 is playing. So I want next time when user opens the list of song, the list row in front of him must starts from row 52 (while up and down are still rows). I tried using

public View getViewByPosition(int pos, ListView listView) {
    final int firstListItemPosition = listView.getFirstVisiblePosition();
    final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

    if (pos < firstListItemPosition || pos > lastListItemPosition ) {
        return listView.getAdapter().getView(pos, null, listView);
    } else {
        final int childIndex = pos - firstListItemPosition;
        return listView.getChildAt(childIndex);
    }
}

and also

adapter.getItem(52);

But both are pointless methods and dont take me to row 52. (These methods are what I used, but might not be the solution) Also I got no solution googling it. Please let me know if you guys can deal with this !!

A big thanks to you for listening!

Upvotes: 3

Views: 4479

Answers (2)

balaji koduri
balaji koduri

Reputation: 1321

try this: link

listview.smoothScrollToPosition(yourPosition);

Upvotes: 1

Anjali
Anjali

Reputation: 2535

This line will select n row starting from top:

listview.setSelectionFromTop(n, 0);

So for your case as you want to select 52 row you have to right following code on start of activity after setting your adapter

listview.setSelectionFromTop(52, 0);

Upvotes: 11

Related Questions