开发者

android Listview Image Loading

hi i have a custom listview that contain an image and textview ,am using handler inside the getview of listview of adapter for loading images , when all the images are load , and then i change the orientation of my phone the image again start to download , i want to block this one,ie when all the images are load ,and when am orientation changes it take no effect.

public View getView(final int position, View convertView, ViewGroup parent) {
        final String imageurl = imageUrls.get(position);
        LayoutInflater inflate = LayoutInflater.from(context);
        View v = inflate.inflate(rowResId, parent, false);
        final ImageView image = (ImageView) v.findViewById(R.id.image);
        final ProgressBar progress = (ProgressBar) v
                .findViewById(R.id.progress);

        image.setVisibility(View.GONE);
        progress.setVisibility(View.VISIBLE);
        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message message) {
                System.out.println("Set image at" + position
                        + " Image Name :: " + imageurl);
                progress.setVisibility(View.GONE);
                image.se开发者_开发问答tImageBitmap((Bitmap) message.obj);
                image.setVisibility(View.VISIBLE);
            }
        };

        Thread thread = new Thread() {
            @Override
            public void run() {
                Bitmap drawable = LoadImageFromWebOperations(imageurl);
                Message message = handler.obtainMessage(1, drawable);
                handler.sendMessage(message);

            }
        };
        thread.start();
        return v;
    }


You can use a list of Bitmap images. At first set all the Bitmap in the list as null. And in the method getView check whether the Bitmap in the position of the Bitmap list is null or not. If null then show the progressBar and download the image and save the image in the list.

For example the images List name is images. Then the code in the getView method will be as follows:

public View getView(final int position, View convertView, ViewGroup parent) {
    final String imageurl = imageUrls.get(position);
    LayoutInflater inflate = LayoutInflater.from(context);
    View v = inflate.inflate(rowResId, parent, false);
    final ImageView image = (ImageView) v.findViewById(R.id.image);
    final ProgressBar progress = (ProgressBar) v
            .findViewById(R.id.progress);
    if(images.get(position) != null) {
         progress.setVisibility(View.GONE);
         image.setImageBitmap(images.get(position));
         image.setVisibility(View.VISIBLE);
    } else {
         image.setVisibility(View.GONE);
         progress.setVisibility(View.VISIBLE);
         final Handler handler = new Handler() {
             @Override
             public void handleMessage(Message message) {
                  System.out.println("Set image at" + position
                    + " Image Name :: " + imageurl);
                  progress.setVisibility(View.GONE);
                  image.setImageBitmap((Bitmap) message.obj);
                  image.setVisibility(View.VISIBLE);
             }
         };

         Thread thread = new Thread() {
         @Override
            public void run() {
               Bitmap drawable = LoadImageFromWebOperations(imageurl);
               images.add(position, drawable);
               Message message = handler.obtainMessage(1, drawable);
               handler.sendMessage(message);
            }
         };
         thread.start();
    }

    return v;
}

Hope, you have understood what I have meant. :)


Just use following method to your Activity

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}


Add this attribute in the android manifest file in the Activity Tag of your activity. No need of any code then.

android:configChanges="orientation|keyboardHidden"

This is a answer to your question, but what is preferable for your issue is what @gypsicoder suggested

Hope this help :)

And don't forget to accept the answer which will help you the most. So that others can also get the right reference.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜