动态加载元素到 ScrollView

发布于 2024-10-08 06:19:00 字数 295 浏览 4 评论 0原文

我正在尝试创建一个动态可滚动视图。它将保留从服务器下载的元素列表。该列表可能有数千个元素。因此,当向下滚动时,元素必须下载到列表中,并且还应该从列表顶部删除以保持较低的内存使用量。我还需要能够将不同的视图设置为元素。

最好使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很糊涂小朋友 2024-10-15 06:19:00

您应该使用 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

半步萧音过轻尘 2024-10-15 06:19:00

这是一个由多个部分组成的问题,但我也许可以帮助解决其中的某些部分。您可以使用 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 设置为随机生成的数字或某种增量索引并以某种方式存储该数字(即 Listint[]),将“旧”元素的 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文