Android ListView滚动间歇性停止

发布于 2024-11-08 19:34:56 字数 1201 浏览 0 评论 0原文

我的 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 技术交流群。

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

发布评论

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

评论(2

同尘 2024-11-15 19:34:56

我很好奇,所以我安装了 NPR 新闻应用程序并复制了你的错误。

据我了解,他们在适配器中缓存来自网络的位图,并且只要缓存需要再次下载这些位图(无论出于何种原因),它就会挂起。问题是,在连续滚动事件中,您需要一次滚动浏览多个列表项,因此,如果其中一个项目需要刷新其缓存值,则其他项目很可能也需要它。根据适配器实现的不同,可能会发生一些奇怪的并发修改。

这是挂起时发生的情况的痕迹:适配器变得非常混乱。

05-21 08:53:07.570: DEBUG/org.npr.android.news.ImageThreadLoader(6457): Cache hit: http://media.npr.org/assets/img/2011/05/20/machoman_sq.jpg?t=1305913905&s=12
05-21 08:53:07.580: DEBUG/org.npr.android.util.PlaylistProvider(6457): content://org.npr.android.util.Playlist;story_id = ?;[136497574]
05-21 08:53:07.580: DEBUG/org.npr.android.news.ImageThreadLoader(6457): Cache hit: http://media.npr.org/assets/img/2011/05/20/pelosi_flex.jpg?t=1305924952&s=12
05-21 08:53:07.610: DEBUG/org.npr.android.news.DownloadDrawable(6457): Got bitmap
05-21 08:53:07.610: DEBUG/org.npr.android.news.DownloadDrawable(6457): Download complete
05-21 08:53:07.610: DEBUG/org.npr.android.news.DownloadDrawable(6457): Starting download
05-21 08:53:07.620: DEBUG/org.npr.android.util.PlaylistProvider(6457): content://org.npr.android.util.Playlist;story_id = ?;[136507004]
05-21 08:53:07.620: DEBUG/org.npr.android.news.ImageThreadLoader(6457): Cache hit: http://media.npr.org/assets/img/2011/05/20/capitol.jpg?t=1305929989&s=12
05-21 08:53:07.630: DEBUG/org.npr.android.util.PlaylistProvider(6457): content://org.npr.android.util.Playlist;story_id = ?;[136498068]
05-21 08:53:07.630: DEBUG/org.npr.android.news.ImageThreadLoader(6457): Cache hit: http://media.npr.org/assets/img/2011/05/20/ppw1.jpg?t=1305913144&s=12
05-21 08:53:07.640: DEBUG/org.npr.android.news.ImageThreadLoader(6457): Cache hit: http://media.npr.org/assets/img/2011/05/20/levee.jpg?t=1305901869&s=12
05-21 08:53:07.670: WARN/org.npr.android.news.NewsListAdapter(6457): Could not find list item at position 27
05-21 08:53:07.680: DEBUG/org.npr.android.news.ImageThreadLoader(6457): Cache hit: http://media.npr.org/assets/img/2011/05/20/imf.jpg?t=1305915244&s=12
05-21 08:53:07.710: DEBUG/org.npr.android.news.ImageThreadLoader(6457): Cache hit: http://media.npr.org/assets/img/2011/05/20/syria.jpg?t=1305904814&s=12
05-21 08:53:07.750: DEBUG/org.npr.android.news.ImageThreadLoader(6457): Cache hit: http://media.npr.org/assets/img/2011/05/20/netanyahu.jpg?t=1305900960&s=12
05-21 08:53:07.850: DEBUG/org.npr.android.news.DownloadDrawable(6457): Got bitmap
05-21 08:53:07.850: DEBUG/org.npr.android.news.DownloadDrawable(6457): Download complete
05-21 08:53:07.860: DEBUG/org.npr.android.news.DownloadDrawable(6457): Starting download
05-21 08:53:07.880: WARN/org.npr.android.news.NewsListAdapter(6457): Could not find list item at position 28
05-21 08:53:08.510: DEBUG/org.npr.android.news.DownloadDrawable(6457): Got bitmap
05-21 08:53:08.510: DEBUG/org.npr.android.news.DownloadDrawable(6457): Download complete
05-21 08:53:08.550: WARN/org.npr.android.news.NewsListAdapter(6457): Could not find list item at position 29

我的建议:使用占位符?让适配器仅从缓存中获取,在未找到占位符时检索占位符,并在缓存无效时触发后台线程操作。

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.

05-21 08:53:07.570: DEBUG/org.npr.android.news.ImageThreadLoader(6457): Cache hit: http://media.npr.org/assets/img/2011/05/20/machoman_sq.jpg?t=1305913905&s=12
05-21 08:53:07.580: DEBUG/org.npr.android.util.PlaylistProvider(6457): content://org.npr.android.util.Playlist;story_id = ?;[136497574]
05-21 08:53:07.580: DEBUG/org.npr.android.news.ImageThreadLoader(6457): Cache hit: http://media.npr.org/assets/img/2011/05/20/pelosi_flex.jpg?t=1305924952&s=12
05-21 08:53:07.610: DEBUG/org.npr.android.news.DownloadDrawable(6457): Got bitmap
05-21 08:53:07.610: DEBUG/org.npr.android.news.DownloadDrawable(6457): Download complete
05-21 08:53:07.610: DEBUG/org.npr.android.news.DownloadDrawable(6457): Starting download
05-21 08:53:07.620: DEBUG/org.npr.android.util.PlaylistProvider(6457): content://org.npr.android.util.Playlist;story_id = ?;[136507004]
05-21 08:53:07.620: DEBUG/org.npr.android.news.ImageThreadLoader(6457): Cache hit: http://media.npr.org/assets/img/2011/05/20/capitol.jpg?t=1305929989&s=12
05-21 08:53:07.630: DEBUG/org.npr.android.util.PlaylistProvider(6457): content://org.npr.android.util.Playlist;story_id = ?;[136498068]
05-21 08:53:07.630: DEBUG/org.npr.android.news.ImageThreadLoader(6457): Cache hit: http://media.npr.org/assets/img/2011/05/20/ppw1.jpg?t=1305913144&s=12
05-21 08:53:07.640: DEBUG/org.npr.android.news.ImageThreadLoader(6457): Cache hit: http://media.npr.org/assets/img/2011/05/20/levee.jpg?t=1305901869&s=12
05-21 08:53:07.670: WARN/org.npr.android.news.NewsListAdapter(6457): Could not find list item at position 27
05-21 08:53:07.680: DEBUG/org.npr.android.news.ImageThreadLoader(6457): Cache hit: http://media.npr.org/assets/img/2011/05/20/imf.jpg?t=1305915244&s=12
05-21 08:53:07.710: DEBUG/org.npr.android.news.ImageThreadLoader(6457): Cache hit: http://media.npr.org/assets/img/2011/05/20/syria.jpg?t=1305904814&s=12
05-21 08:53:07.750: DEBUG/org.npr.android.news.ImageThreadLoader(6457): Cache hit: http://media.npr.org/assets/img/2011/05/20/netanyahu.jpg?t=1305900960&s=12
05-21 08:53:07.850: DEBUG/org.npr.android.news.DownloadDrawable(6457): Got bitmap
05-21 08:53:07.850: DEBUG/org.npr.android.news.DownloadDrawable(6457): Download complete
05-21 08:53:07.860: DEBUG/org.npr.android.news.DownloadDrawable(6457): Starting download
05-21 08:53:07.880: WARN/org.npr.android.news.NewsListAdapter(6457): Could not find list item at position 28
05-21 08:53:08.510: DEBUG/org.npr.android.news.DownloadDrawable(6457): Got bitmap
05-21 08:53:08.510: DEBUG/org.npr.android.news.DownloadDrawable(6457): Download complete
05-21 08:53:08.550: WARN/org.npr.android.news.NewsListAdapter(6457): Could not find list item at position 29

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.

风吹雪碎 2024-11-15 19:34:56

尝试使用 DroidDraw 解决所有布局问题。它是一个用于创建 Android 布局的拖放桌面应用程序。每当我坚持布局时我总是使用它。我认为你应该在布局中使用 Tablelayout 并将按钮和列表放在 Tablerow 中。

这是我使用一个按钮和列表视图为我的应用程序创建的示例布局

<?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/apps"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >
                        <Button
                            android:id="@+id/undoApps"
                            android:layout_height="60dip"
                            android:layout_width="320dip"
                            android:text="I AM A BUTTON"
                            android:textSize="22sp"
                            android:textStyle="bold"
                            android:typeface="serif">
                        </Button>
             </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >
                <ListView
                    android:id="@android:id/list"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:drawSelectorOnTop="false" >
                </ListView>
            </TableRow>
   </TableLayout>

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 in Tablerow.

Here is a sample layout which i had created for my app with one button and listview

<?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/apps"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >
                        <Button
                            android:id="@+id/undoApps"
                            android:layout_height="60dip"
                            android:layout_width="320dip"
                            android:text="I AM A BUTTON"
                            android:textSize="22sp"
                            android:textStyle="bold"
                            android:typeface="serif">
                        </Button>
             </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >
                <ListView
                    android:id="@android:id/list"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:drawSelectorOnTop="false" >
                </ListView>
            </TableRow>
   </TableLayout>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文