动态加载元素到 ScrollView
我正在尝试创建一个动态可滚动视图。它将保留从服务器下载的元素列表。该列表可能有数千个元素。因此,当向下滚动时,元素必须下载到列表中,并且还应该从列表顶部删除以保持较低的内存使用量。我还需要能够将不同的视图设置为元素。
最好使用 ScrollView 还是 ListView?我将如何向列表添加元素并控制何时下载更多元素?
我记得偶然发现了一个关于如何执行此操作的示例或教程。特别是关于如何在向下滚动时添加元素、删除顶部元素以及创建不同的 xml 视图以用作元素。我现在找不到这个示例教程。我认为这是关于如何重用元素,因为它们在滚动时会超出范围。
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 about 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用 ListView,这正是它的用途,并且它的作用正是您所描述的。 :)
有关详细信息,请参阅此内容:http:// /www.google.com/events/io/2010/sessions/world-of-listview-android.html
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
这是一个由多个部分组成的问题,但我也许可以帮助解决其中的某些部分。您可以使用 ScrollView,我更喜欢这样做,因为它允许您定义任意数量的 UI 元素。
添加/删除元素
首先,我将为每个单独的列表元素创建一个 XML 布局。当您需要添加元素时,您可以像这样膨胀此布局:
LinearLayout clone = (LinearLayout)View.inflate(this, R.layout.sample, null)
然后您可以设置元素的信息通过访问您膨胀的 XML 中的小部件
clone.findViewById(R.id.NameSpace).setText("This is element Johnny")
我会将克隆的 ID 设置为随机生成的数字或某种增量索引并以某种方式存储该数字(即
List
或int[]
),将“旧”元素的 ID 保留在存储设备的前面。然后,当您需要访问元素以删除它们时,只需为第一个元素调用ScrollView.removeView( findViewById( Storage.get(0) ) )
即可。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
orint[]
), 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 callScrollView.removeView( findViewById( Storage.get(0) ) )
for the first element.