简体   繁体   中英

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM