安卓:为什么线程在getView()似乎并没有工作? [英] Android: why Thread in getView() does not seem to work?

查看:95
本文介绍了安卓:为什么线程在getView()似乎并没有工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这(在 getView())上班......

I would expect this (inside getView()) to work...

但没有任何反应时,位图不加载到ImageView的......

but nothing happens, the bitmaps are not loaded into the ImageView....

Thread th= new Thread(new Runnable() {

           @Override
           public void run() {

               ImageLoader.getInstance().displayImage(rss.getChannel().getItems().get(position).getEnclosure().getUrl(),holder.imageView,options,new ImageLoadingListener() {
                   @Override
                   public void onLoadingStarted(String imageUri, View view) {
                   //...stuff...
                   }

                   //... more stuff

                   @Override
                   public void onLoadingCancelled(String imageUri, View view) {
                  //...stuff
                  }
               });

           }
       });

th.start();

请它为什么不???

感谢: - )

推荐答案

什么你试图做的是非常糟糕的做法。但是,我不能告诉你,从你提供给我们的问题是什么的信息。 我认为ImageLoading库已经在做的事情在自己的线程,因此您的线程已经完成。也只有在主UI线程可以访问或操控UI的观点,但应该有抛出,如果这将是问题的一个例外。

What you try to do is really bad practice. However, I cant tell you from the information you provide us what the problem is. I think that the ImageLoading library is already doing things in its own thread, therefore your Thread is already finished. Also only the main UI Thread can access or manipulate UI Views, but there should be an exception thrown if this would be the problem.

此外,我会建议你使用毕加索。它处理的ListView或RecyclerView Threadpooling,调度,缓存,内存管理(避免使用WeakReference的内存泄漏)和回收视图。

Furthermore, I would recommend you to use Picasso. It handles Threadpooling, scheduling, caching, memory management (avoid memory leaks by using WeakReference) and recycling view in ListView or RecyclerView.

更新:

您正在创建一个每次 getView()被滚动时调用。这是不好的做法,因为创建一个是昂贵的。想想丢或快速滚动列表。如果您滚动20个要素,20个线程将被创建并启动。所有的纤维性具有相同的优先级在主UI线程。这意味着,在CPU 20线程加上主UI线程之间共享。这可能会导致糟糕的用户界面的性能。您应该使用线程池。此外,你不停止你的主题。这带来两个问题:

You are creating a Thread everytime getView() is called while scrolling. This is bad practice, because creating a Thread is expensive. Think about flinging or fast scrolling a list. If you scroll 20 elements, 20 Threads will be created and started. All the thready have the same priority as the main UI thread. That means that the CPU is shared between 20 Threads plus the main UI Thread. This could lead to bad UI performance. You should use a ThreadPool. Furthermore, you don't stop your Thread. This brings two problems:

  1. 您的观点得到回收的 getView()而线程不是,所以线程仍在运行,而认为应该已经显示其他项目。
  2. 您获得内存泄漏!你知道如何垃圾收集工作的?它开始于线程级和搜索未使用的对象(对象不被任何其他对象不再引用)。你的线程是一个内部类。因此,它有一个硬引用周围的适配器类。该适配器连接到的ListView 的ListView 来在活动。所以,如果你的活动被用户关闭的垃圾收集器不能收集活动,所有的意见和其他对象,直到线程(或在您的情况下,所有线程)完成。你可能耗尽内存。
  1. Your view get recycled in getView() while the Thread is not, so the thread is still running while the view should display already another item.
  2. You get memory leaks! Do you know how Garbage Collection works? It starts at thread level and searches for unused objects (objects that are not referenced anymore by any other object). Your Thread is a inner class. Therefore it has a hard reference to the surrounding Adapter class. The Adapter is attached to the ListView, the ListView to the Activity. So if your Activity gets closed by the user the garbage collector can not collect Activity and all the views and other objects until the Thread (or in your case, all Threads) are finished. You probably run out of memory.

最后但并非最不重要的,Android系统有其自己的消息队列基于线程的系统,只在主UI线程可以访问用户界面视图。我不想去深在这里详细,我敢肯定,你可以google一下。在这一点上,我建议使用 AsyncTask的如果你真的要开始你的自己的线程。但是,你也可以手动取消的AsyncTask以避免内存泄漏。

Last but not least, the Android System has its own message queue based threading system and only the main UI Thread can access UI Views. I don't want to get to deep in detail here, I'm sure you can google it. At this point I recommend to use AsyncTask if you really have to start your own Thread. But you also have to cancel the AsyncTask manually to avoid memory leaks.

这篇关于安卓:为什么线程在getView()似乎并没有工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆