【问题标题】:ListView with drawable images is laggy具有可绘制图像的 ListView 滞后
【发布时间】:2014-08-29 15:13:38
【问题描述】:

找到解决方案(感谢您的回答!) 这是绘图文件的大文件大小(.PNG)

我的 /drawables 文件夹中有一个列表视图和许多 .png 图像。当我在列表视图项的图像视图中加载这些图像时,列表视图的滚动变得迟缓

public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.drawer_list_item, null);
            holder = new ViewHolder();
            holder.title = (TextView) convertView.findViewById(R.id.title);
            holder.store_image = (ImageView) convertView.findViewById(R.id.store_image);
            convertView.setTag(holder);
        }
        else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.title.setText(stores[position]);
        switch(position){
                case 0: Picasso.with(context).load(R.drawable.first).into(holder.store_image);break;
                ///... more cases ...
                default: break;
                }
            }

        return convertView;

    }

我在 NavigationDrawer 中实现这个

编辑:

我也尝试过这种创建和绘制数组然后设置图像视图的方法,但仍然滞后。

//in the adapter
int[] store_images = new int[]{R.drawable.first,...and so on}; 
// and then in getView()
holder.store_image.setImageResource(store_images[position]);

【问题讨论】:

  • 很有趣,因为这两个属性会使情况变得更糟:)
  • 你能解释一下这个方法 Picasso.with(context).load() 做了什么
  • @user3249477 我删除了这两个属性仍然很滞后。
  • @AjitPratapSingh picasso 是一个加载图像的库。在github上搜索
  • 一个drawable的文件大小为19KB。

标签: java android android-listview


【解决方案1】:

编辑代码

试试这个代码!!

           ArrayList<Boolean> alDone= new   ArrayList<Boolean>();

                public View getView(final int position, View convertView, ViewGroup parent) {
                                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                                final ViewHolder holder;
                                if (convertView == null) {
                                    convertView = inflater.inflate(R.layout.drawer_list_item, null);
                                    holder = new ViewHolder();
                                    holder.title = (TextView) convertView.findViewById(R.id.title);
                                    holder.store_image = (ImageView) convertView.findViewById(R.id.store_image);
                                    convertView.setTag(holder);
                                }
                                else{
                                    holder = (ViewHolder) convertView.getTag();
                                }

                                holder.title.setText(stores[position]);

                               if(position==0){

                                if(!alDone.get(position)){
    Picasso.with(context).load(R.drawable.first).into(holder.store_image);
alDone.add(true,position)
        }
                    }
                                return convertView;

                            }

【讨论】:

  • 我删除了线程!我使用的是 switch case 而不是 if() 因为我有多个图像要显示。但仍然滞后
  • 不要每次都加载毕加索。一旦为该位置加载了图像,就不要再次加载它。我已经编辑了代码。我希望它有所帮助。
  • 我得到 java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 (at the if(!aldone) statement)
【解决方案2】:

您使用充气机的方式不正确。

试试这个:

 convertView = inflater.inflate(layoutResourceId, parent, false);

http://www.doubleencore.com/2013/05/layout-inflation-as-intended/

这可能有助于延迟。 Picasso 是一个很好的框架,会为你缓存图片,所以这可能不是原因。

祝你好运!

【讨论】:

    【解决方案3】:

    好的!所以问题是 19kb 的文件大小!我将文件大小减小到 5-6kb,延迟消失了!尽管如果您遇到与我相同的问题,其他人的答案也会有所帮助!

    【讨论】:

      【解决方案4】:

      导致列表视图滞后的原因有很多:

      1. 您的 xml 布局:您使用的是 RelativeLayout 还是嵌套布局?然后在滚动时进行布局可能是问题所在。解决方案:平面布局
      2. 在我看来,您似乎尝试实现不同的视图类型。因此,将您的布局 xml 文件一分为二并定义两种视图类型。一个用于列表中的第一个元素,一个用于其他元素。
      3. 您是否加载了大量图像?那么垃圾收集可能是问题所在,因为它会阻塞 UI 线程,直到 GC 完成。检查日志猫。在这里您会发现 GC 运行了多少次以及需要多少时间。正如我所看到的,您使用毕加索。有一个功能还不是毕加索的一部分(仍然是一个拉取请求),它通过向 ListView / RecyclerView 添加一个 OnScrollListener 来处理这个问题:https://github.com/square/picasso/pull/561

      【讨论】:

        【解决方案5】:

        如果您不从网络加载图像,请不要使用 Picasso。你的代码:

        Picasso.with(context).load(R.drawable.first).into(holder.store_image);
        

        表明您已经将图像存储在您的应用中,因此只需将它们直接加载到您的 ImageView 中。

        另一方面,来自ImageView#setImageResource 上的文档:

        这在 UI 线程上进行 Bitmap 读取和解码,可以 导致延迟打嗝。如果这是一个问题,请考虑使用 setImageDrawable(android.graphics.drawable.Drawable) 或 setImageBitmap(android.graphics.Bitmap) 和 BitmapFactory 代替。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-28
          相关资源
          最近更新 更多