user386430
user386430

Reputation: 4967

How to bring selected list item position in centre of listview in android

How to bring selected list item position in centre of lisview in android I need to use this method public void smoothScrollByOffset (int offset)

How to find offset?

Upvotes: 0

Views: 756

Answers (1)

Sound Conception
Sound Conception

Reputation: 5411

I think what you actually want is:
smoothScrollToPositionFromTop (int position, int offset) This will scroll the list item you indicate with position so that it is offset pixels below the top edge of the ListView.

To make it scroll to the center you could do something like:

int listViewHeight = myListView.getMeasuredHeight();
myListView.smoothScrollToPositionFromTop(myItemPosition, (listViewHeight / 2));

This will put the top edge of your chosen item half way down the ListView.

If you wanted to go further, you could also get the measured height of the item's view and subtract half of that from the offset too. This would truly "center" the item at the mid height of the ListView.

Upvotes: 1

Related Questions