Java 具有可绘制图像的ListView是滞后的

Java 具有可绘制图像的ListView是滞后的,java,android,android-listview,Java,Android,Android Listview,找到解决方案(感谢您的回答!) 这是DRAWABLES(.PNG)的高文件大小。 我的/drawables文件夹中有一个listview和许多.png图像。当我在listview项目的imageview中加载这些图像时,listview的滚动会变慢 public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) co

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

我的/drawables文件夹中有一个listview和许多.png图像。当我在listview项目的imageview中加载这些图像时,listview的滚动会变慢

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中实现此功能

编辑:

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

//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]);
编辑代码

试试这个代码

           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;

                            }
ArrayList alDone=new ArrayList();
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
最终持票人;
if(convertView==null){
convertView=充气机。充气(R.layout.drawer\u list\u项,空);
holder=新的ViewHolder();
holder.title=(TextView)convertView.findViewById(R.id.title);
holder.store\u image=(ImageView)convertView.findViewById(R.id.store\u image);
convertView.setTag(支架);
}
否则{
holder=(ViewHolder)convertView.getTag();
}
holder.title.setText(存储[位置]);
如果(位置==0){
如果(!alDone.get(位置)){
毕加索.with(context).load(R.drawable.first).into(holder.store\u image);
添加(正确,位置)
}
}
返回视图;
}

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

试试这个:

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

这可能有助于缓解滞后。毕加索是一个非常好的框架,可以为您缓存图像,所以这可能不是原因


祝你好运

好的!所以问题是19kb的文件大小!我将文件大小减少到5-6kb,延迟消失了!虽然如果你和我有同样的问题,其他人的回答也会有帮助

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

  • 您的xml布局:使用RelativeLayout还是嵌套布局?那么滚动时的布局可能就是问题所在。解决方案:平面布局
  • 在我看来,您似乎试图实现不同的视图类型。因此,将布局xml文件一分为二,并定义两种视图类型。一个用于列表中的第一个元素,另一个用于其他元素
  • 你有很多图片吗?那么垃圾收集可能就是问题所在,因为它会在GC完成之前阻塞UI线程。检查日志。在这里,您将发现GC运行了多少次以及需要多少时间。我见过你用毕加索。有一个功能不是毕加索的一部分(仍然是一个拉请求),它通过向ListView/RecyclerView添加OnScrollListener来处理此问题:

  • 如果你没有从网上下载图片,就不要使用毕加索。您的代码:

    Picasso.with(context).load(R.drawable.first).into(holder.store_image);
    
    建议您已经将图像存储在应用程序中,因此只需将其直接加载到ImageView中即可

    另一方面,从上的文档:

    这在UI线程上执行位图读取和解码,可以 引起打嗝。如果这是一个问题,考虑使用 setImageDrawable(android.graphics.drawable.drawable)或 改为设置ImageBitmap(android.graphics.Bitmap)和BitmapFactory


    有趣的是,这两个属性都会让事情变得更糟:)你能解释一下在这个方法中毕加索做了什么吗?with(context).load()@user3249477我删除了这两个属性,但仍然是它的laaggy。@Ajitpratasingh-Picasso是一个加载图像的库。在Github上搜索一个drawable的文件大小是19KB。我删除了线程!我使用switch case而不是if(),因为我要显示多个图像。但它仍然很落后。不要每次都加载毕加索。一旦图像加载到该位置,就不要再加载它。我编辑了代码。我希望它能有所帮助。我得到java.lang.IndexOutOfBoundsException:索引0无效,大小为0(在if(!aldone)语句中)