LinearLayout/ScrollView 中的无限滚动?
我知道可以以某种方式创建一个 ListView,当用户到达列表底部时加载更多数据。但是,我正在使用 ScrollView,其中有 LinearLayout,这两个组件在滚动等方面效果很好。但我不知道该怎么做才能让它无限滚动。
我想我需要添加一些内容来读取屏幕上 LinearLayout 的显示内容,并且当它计算出正在显示的 LinearLayout 的底部时(通过使用当前位置和视图的高度),它触发事件。
但正如我所说,我不知道如何实现这一点。所以,如果有人能给我一些帮助,我将非常感激。
编辑:我在 StackOverflow Android如何在scrollView到达底部时触发事件?,但我不知道如何处理答案:
根据要求,您可能会扩展 BaseAdapter(而不是使用不同机制的 CursorAdapter)。
这是一个片段:
public View getView(intposition,ViewconvertView,ViewGroupparent){ if (position == backingLinkedList.size()) { //获取更多项目并将它们添加到后台线程中的 backingLinkedList 中 通知数据集更改(); } }
I know it's somehow possible to make a ListView that loads more data when the user has reached the bottom of the list. However, I'm working with a ScrollView, which I have a LinearLayout in, and these two components works great with the scrolling and so. But I don't know how I'm supposed to do so it gets an infinite scroll.
I suppose I need to add something that reads what is shown of the LinearLayout on the screen, and when it calculates that it is the bottom of the LinearLayout that is being shown (by using the current position and the height of the View), it triggers an event.
But as I said, I don't know how to accomplish this. So, if anyone can give me some help I would be very grateful.
EDIT: I found this post here on StackOverflow How to trigger an event when scrollView reach the bottom with Android?, but I don't know what to do with the answer:
Given the requirements, you'll likely be extending BaseAdapter (as opposed to CursorAdapter which utilizes a different mechanism).
Here's a snippet for that:
public View getView(int position, View convertView, ViewGroup parent) { if (position == backingLinkedList.size()) { //get more items and add them to the backingLinkedList in a background thread notifyDataSetChanged(); } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您可以在到达滚动视图底部时触发事件,您可以尝试创建自己的视图类型来保存数据。当您到达视图的底部时,将下一个数据加载到视图类中并将其添加到滚动视图的布局中。
因此,基本上创建一个类来保存您所展示内容的一部分。将第一个添加到滚动视图中。当您到达底部时,创建另一个视图来保存下一个数据并使用
scrollviews_layout.addview(view_holding_the_new_data)
If you can trigger an event when you reach the bottom of a scrollview, You could try creating your own type of view to hold your data. When you reach the bottom of the view load the next data into your view class and add it into the layout of the scroll view.
So basically create a class that holds a section of what your showing. Add the first one into the scrollview. When you hit the bottom create another view holding your next data and use
scrollviews_layout.addview(view_holding_the_new_data)
我知道这个问题很久以前就已经解决了,但我想分享我对未来无限滚动条的解决方案。
我使用
OnScrollListener
中的onScroll
方法触发后台线程以获取更多数据,并使用adapter.notifyDataSetChanged
来通知ListView< /code> 加载更多数据并创建无限滚动。由于某种原因,
onScrollStateChanged
没有触发,但onScroll
做了我需要的事情,所以我不需要打扰。在很长一段时间里,我不明白如何防止
ListView
重新加载并将我放回列表顶部,但后来我意识到我只需要初始化一个新的适配器并调用setListAdapter
一次,此后每次仅调用notifyDataSetChanged
。之前,我还调用了setListAdapter
,从而使我回到了顶部。I know this was solved a long time ago, but I wanted to share my solution for future infinite scrollers.
I used the
onScroll
method within theOnScrollListener
to trigger a background thread to grab more data andadapter.notifyDataSetChanged
to notify theListView
of more data to load and create an infinite scroll. For some reason,onScrollStateChanged
wasn't triggering, butonScroll
did what I needed so I didn't need to bother.For the longest time I didn't understand how to keep the
ListView
from reloading and placing me back at the top of the list, but then I realized I only need to initialize a new adapter and callsetListAdapter
once, and every time after that, only callnotifyDataSetChanged
. Before, I was also callingsetListAdapter
, thereby placing me back at the top.