【问题标题】:How to prevent flickering ListView when requesting Layout very often经常请求Layout时如何防止ListView闪烁
【发布时间】:2014-09-19 17:31:31
【问题描述】:

我正在实现一个“视差”效果,它由上面的列表视图和图像视图组成。 当用户向下滚动时,偏移量会从图像视图的高度中减去。这工作正常。我在列表视图的滚动侦听器中执行此操作。要真正实时更新列表视图和图像视图,我需要致电requestLayout()

问题是,这个方法是异步的,如果我慢慢滚动,它会极度闪烁。如果我只是快速滑动它看起来非常好。有没有其他解决方案来解决这个问题?

滚动监听器

@Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            View c = view.getChildAt(0);
            int top = (c == null) ? 0 : (-c.getTop() + view.getFirstVisiblePosition() * c.getHeight());
            //Log.d(TAG, "Offset = " + top);
            mImageView.getLayoutParams().height = 400 - top;
            mImageView.requestLayout();
        }

布局

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:context="xxx.fragments.NewsFragment">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="@android:color/holo_blue_light"
    android:id="@+id/advertisement"
    />

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/news_feed">

</ListView>

</TableLayout>

【问题讨论】:

  • 所以 imageView 应该是列表的一部分,但是有视差?您能否详细说明您是如何使其成为列表的一部分的?
  • ImageView 不在列表中。它位于 listView 上方的 TableLayout 中,只需缩小 imageView 的高度(直到它消失),listView 在 TableLayout 中向上移动。也许这不是“真正的视差”,但这基本上就是它在行动中所看到的。这就是我试图描述情况的方式

标签: android performance android-layout listview


【解决方案1】:

如果你只是想让 ImageView 随列表滚动,那么你可以简单地add it as a header view:

listView.addHeaderView(mImageView);

确保在调用 setAdapter 之前调用此方法。还要确保您的 ImageView 还没有父级,因此单独对其进行扩充或使用 new ImageView 在代码中创建一个。

【讨论】:

  • 这正是我想要实现的......该死的:D
猜你喜欢
  • 2013-08-04
  • 1970-01-01
  • 2010-09-10
  • 2020-05-07
  • 1970-01-01
  • 1970-01-01
  • 2011-01-30
  • 2014-01-06
相关资源
最近更新 更多