新主题,而在片段加载的ListView [英] New Thread while loading ListView in a Fragment

查看:243
本文介绍了新主题,而在片段加载的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动,它拥有TabHolder。每个标签的片段。

I have an Activity, which holds a TabHolder. Every tab its a Fragment.

有一个标签,有一个ListView,它应该有照片和文字得到满足。问题是,当我在标签中,点击,用户界面​​需要一段时间来加载,因为我已经拿到照片由URI,调整位图到的ImageView在ListView的适配器,和present它。我想放照片的加载在一个线程,和present一个ProgressDialog给用户,而照片的负荷。但我不知道在哪里!如果我在适配器做它在getView方法,它开始出现异常,因为它被称为多次在阵列中的照片...在OnCreateView()试图这样做,里面的片段:

One of the tabs, has a ListView, which should be fulfilled with photos, and text. The problem is, when I tap in the tab, the UI takes a while to load, because I have to get the photos by the URI, resize the bitmap to an ImageView in the adapter in the ListView, and present it. I would like to put the loading of the photos in a thread, and present a ProgressDialog to the user, while the photographs loads. But I don't know where! If I do it in getView method in the adapter, it starts to behave strangely, because it is called as many times as photos in the array...tried doing this in OnCreateView(), inside the Fragment:

adapter=new FotoAdapter(getActivity(), arrayFotos);
    final ProgressDialog dialog=ProgressDialog.show(getActivity(), getString(R.string.onemoment), getString(R.string.fotoloading));
    handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==1){
                dialog.dismiss();
            }
        }
    };

    Thread thread=new Thread(new Runnable() {
        @Override
        public void run() {
            listaFotos.setAdapter(adapter);
            Message message=new Message();
            message.what=1;
            handler.sendMessage(message);
        }
    });
    thread.start();

...但它什么都不做,没有显示对话框,并采取同一时间present的UI给用户。

...but it does nothing, no dialog is shown, and takes the same time to present the UI to the user.

我怎么能实现呢?

感谢您。

编辑:

建议后,尝试了毕加索。在这里,你可以看到这两种方法,与毕加索,没有它:

After suggestion, tried Picasso. Here you can see both approaches, with Picasso and without it:

String path=fotos.get(position).getFotoPath();
//Picasso.with(c).load(path).resize(80, 80).into(holder.foto);
Bitmap foto= BitmapFactory.decodeFile(path);
int bmWidth=foto.getHeight();
int bmHeight=foto.getHeight();
int ivWidth;
int ivHeigth;
int new_width;
int new_height;
ivWidth=dpToPx(80);
new_width=ivWidth;
new_height = (int) Math.floor((double) bmHeight *( (double) new_width / (double) bmWidth));
Bitmap newbitMap = Bitmap.createScaledBitmap(foto, new_width, new_height, true);
holder.lat.setText(fotos.get(position).getLatitud().toString());
holder.lon.setText(fotos.get(position).getLongitud().toString());
holder.foto.setImageBitmap(newbitMap);

使用毕加索,我无法看到的ImageView图像。

Using Picasso, I cannot see the image in the ImageView.

推荐答案

一个。不要对一个线程不是UI线程的任何视图(如u一样)。

A. Don't set any views on a thread that isn't the UI thread (as u did).

乙。使用毕加索获取图像加载或其他一些lib下,不推倒重来。的lib将尽一切背部线程加载的,因此你不需要任何进度(可能)。

B. Use Picasso for image loading or some other lib, don't reinvent the wheel. The lib will do all of the loading on back thread therefore you wouldn't need any progressbar (probably).

请按照这两个步骤,你的code应该可以正常工作。

Follow those two steps and your code should work fine.

编辑:
问题是你可能的路径,我用毕加索从内部存储无法加载的URL。打印您的路径,并一起来看看。

The issue is your path probably, I've used picasso to load urls not from inner storage. Print your path and take a look.

这篇关于新主题,而在片段加载的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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