简体   繁体   中英

How to make new inserted rows become on top of ListView (Android Studio)

The usual thing when I insert a new row into a ListView it is added at the bottom of the ListView by default. Can I make it upside down? I want the new added rows be on top, or I may say I want the ListView in descending flow sorted by time. Is there a way to implement that from ListView settings? or it is related to the List connected with the ListView? I searched about it and all I could find is "stackFromBottom" which makes the view of ListView scroll down and start showing from the bottom, and it is different from what I want.

you can add row to top using add function with index.

example :

items.add(0, obj)

inside adapter you have item list that you add the row to top(index 0) and call

notifyiteminserted or notifydatasetchanged

probably duplicated Add new items to top of list view on Android?

You can do it in several way

1.You can use Collections Feature. After add item in your List then call Collections.reverse() then update your ListView

2.You can set your List to add(0,value) 0 no position then update your 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