New Thread while loading ListView in a Fragment

∥☆過路亽.° 提交于 2019-12-11 09:15:29

问题


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

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();

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

How could I achieve it?

Thank you.

EDIT:

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);

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


回答1:


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

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).

Follow those two steps and your code should work fine.

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




回答2:


In case someone is trying to load images from the device, and is facing the same problem that me, this is the correct syntax to use Picasso with that images:

Picasso.with(c).load(new File(path).resize(80, 80).into(holder.foto);


来源:https://stackoverflow.com/questions/31700716/new-thread-while-loading-listview-in-a-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!