Android ListView阻止UI

时间:2015-08-24 19:28:30

标签: android multithreading listview

我有ListView适配器的以下代码!它工作正常但是当它从服务器加载图像时它会阻止UI。我把BitmapFactory.decodeStream()方法放在单独的线程中,然后它只为listview中的第一个对象加载一个图像任何想法?

 @Override
    public int getCount() {
        return applicationList.size();
    }

    @Override
    public ApplicationPojo getItem(int position) {
        return applicationList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View view, ViewGroup parent)
    {

        if (view == null)
        {
            holder = new ViewHolder();

            view = inflater.inflate(R.layout.singleapp, null);
            holder.appName = (TextView) view.findViewById(R.id.appName);
            holder.developer = (TextView) view.findViewById(R.id.developer);
            holder.rate = (TextView) view.findViewById(R.id.rateApp);

            view.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) view.getTag();
        }

        try
        {
            final View finalView = view;

            holder.logo = (ImageView) finalView.findViewById(R.id.appLogo);
            logoName = applicationList.get(position).getLogo();

            logoImg = BitmapFactory.decodeStream((InputStream) new URL(IPClass.SERVERIP + logoName).getContent());

            holder.logo.setImageBitmap(logoImg);
            holder.appName.setText(applicationList.get(position).getName());
            holder.developer.setText(applicationList.get(position).getDeveloper());
            holder.rate.setText(String.valueOf(applicationList.get(position).getRate()));
        }
        catch (Exception ex)
        {
            Log.d("Adapter Exception", ex.toString());
        }
        return view;
    }

    public class ViewHolder
    {
        ImageView logo;
        TextView appName;
        TextView developer;
        TextView rate;
    }

2 个答案:

答案 0 :(得分:0)

您是否也处理退避,缩放,内存和缓存?我强烈建议使用GetForegroundWindowPicasso.with(context).load("http://www.url.com/image.png").into(holder.logo);

答案 1 :(得分:0)

嘿,我更喜欢这个解决方案,需要在列表视图中加载位图: 下载映像后,将位图添加到缓存中。

http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

在后台下载图片,一旦下载,请显示并添加到缓存中。 在" listview getView"的下一次迭代中在某个位置,检查位图是否已经在缓存列表中并从那里加载。