开发者

Dynamically load elements to a ScrollView

I'm trying to create a dynamical scrollable view. It will keep a list of elements downloaded from a server. This list could be thousands of elements. Because of this elements have to be downloaded to the list as it is scrolled downwards, and also should be deleted from the top of the list to keep memory usage low. I also need to be able to set different views as elements.

Would it be best to use a ScrollView or a ListView? And how would I go about adding elements to a list and keeping control of when to download more elemets?

I remember stumbling across an example or tutorial about how to do this. Especially abo开发者_高级运维ut how to add elements as it is scrolled down, delete elements at the top, and create different xml views to use as elements. I cannot find this example tutorial now. It was something about how to reuse elements as they go out if scope when scrolled I think.


You should use a ListView, this is exactly what it is for and it does exactly what you describe. :)

See this for more info: http://www.google.com/events/io/2010/sessions/world-of-listview-android.html


This is quite a multi-part question, but I may be able to help with some part of it. You can use a ScrollView, and I prefer to do it that way as it allows you to define as many UI elements as you want.


Adding/Removing Elements

First I'd create an XML layout for each individual list element. When you need to add an element, you can inflate this layout like so:

LinearLayout clone = (LinearLayout)View.inflate(this, R.layout.sample, null)

Then you can set the element's information by accessing widgets in the XML you inflated

clone.findViewById(R.id.NameSpace).setText("This is element Johnny")

I would set the clone's ID to a randomly generated number or some kind of incremental index and store that number somehow (ie a List or int[]), keeping the ID of the "older" elements in the front of the storage device. Then, when you need to access elements to remove them you can just call ScrollView.removeView( findViewById( Storage.get(0) ) ) for the first element.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜