Android ListView滚动间歇性停止
我的 Activity 有一个自定义标题栏和一个 ListView。 ListView 使用 SimpleAdapter 显示数据数组,没什么花哨的。我注意到,当我在不离开屏幕的情况下不断上下滑动手指时,滚动通常会停止并且滚动条会消失。 Gmail 收件箱活动中永远不会发生这种情况。在 NPR 新闻应用程序中,菜单活动也遇到同样的问题。我错过了什么?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/title_bar"
android:singleLine="true"
android:textAppearance="@android:style/TextAppearance.WindowTitle">
<Button
android:id="@+id/accounts_filter"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
style="@android:style/Widget.Button.Small"
android:maxWidth="200sp"
android:singleLine="true"
android:ellipsize="marquee"
android:onClick="openFilter"/>
</RelativeLayout>
<ListView android:id="@+id/accounts_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
My Activity has a custom title bar and a ListView. The ListView uses a SimpleAdapter showing an array of data, nothing fancy. I noticed that when I keep sliding my finger up and down without leaving the screen, quite often the scrolling stops and the scrollbar disappears. This never happens in Gmail Inbox activity. In NPR News app, the Menu activity suffers the same issue. What did I miss?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/title_bar"
android:singleLine="true"
android:textAppearance="@android:style/TextAppearance.WindowTitle">
<Button
android:id="@+id/accounts_filter"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
style="@android:style/Widget.Button.Small"
android:maxWidth="200sp"
android:singleLine="true"
android:ellipsize="marquee"
android:onClick="openFilter"/>
</RelativeLayout>
<ListView android:id="@+id/accounts_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很好奇,所以我安装了 NPR 新闻应用程序并复制了你的错误。
据我了解,他们在适配器中缓存来自网络的位图,并且只要缓存需要再次下载这些位图(无论出于何种原因),它就会挂起。问题是,在连续滚动事件中,您需要一次滚动浏览多个列表项,因此,如果其中一个项目需要刷新其缓存值,则其他项目很可能也需要它。根据适配器实现的不同,可能会发生一些奇怪的并发修改。
这是挂起时发生的情况的痕迹:适配器变得非常混乱。
我的建议:使用占位符?让适配器仅从缓存中获取,在未找到占位符时检索占位符,并在缓存无效时触发后台线程操作。
I was curious so I installed the NPR news app and got to replicate your bug.
From what I understand they are caching Bitmaps from the web in their adapter, and it hangs whenever the cache needs to download those bitmaps again (for whatever reason). The thing is, on continuous scrolling events you're expected to scroll through multiple list items at once, so if one item needs to refresh its cached value there is a good chance that the other items need it too. Depending on the adapter implementation there might be some weird concurrent modification happening.
here's the trace of what happens when it hangs: the adapter is getting massively confused.
My suggestion: use placeholders? Have the adapter fetch from the cache only, retrieve a placeholder when it's not found, and trigger a background thread operation when the cache is invalid.
尝试使用 DroidDraw 解决所有布局问题。它是一个用于创建 Android 布局的拖放桌面应用程序。每当我坚持布局时我总是使用它。我认为你应该在布局中使用
Tablelayout
并将按钮和列表放在Tablerow
中。这是我使用一个按钮和列表视图为我的应用程序创建的示例布局
Try using DroidDraw for all your layout problems. Its a drag and drop desktop app for creating android layouts. I always use it whenever i stuck with layouts. And i think you should use
Tablelayout
in your layout and put button and list inTablerow
.Here is a sample layout which i had created for my app with one button and listview