简体   繁体   中英

GetView skip first image to be displayed due to repetition of position 0 three times

I am trying to load image in my gallery from Url using lazy load and everything works fine. It shows a ProgressBar until the image loads and gone when image is loaded from site. I have tried all the ways but it skip the first image to load and show remaining images perfectly as per my requirement. I have checked log and the result is that getView method shows position 0 0 0 1 0 0 0 1 if i have two images so it does not show first image as it is calling three times simultaneously causing it to reload before loading. So my question is why it is calling it three times at once. I have searched many question regarding this but there sequences are 0 1 0 1 and that is not a problem. Why is it happening?

Below is my some code.

This is my custom ImageView

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

public class WebImageView extends ImageView {

    private Drawable image;
    private ProgressBar placeholder;

    public WebImageView(Context context) {
        super(context);
    }
    public WebImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    public WebImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setPlaceholderImage(int resid) {
        View view = (View)this.getParent();
        placeholder = (ProgressBar)view.findViewById(resid);
        if (image == null) {
            placeholder.setVisibility(View.VISIBLE);
        }
    }

    @SuppressWarnings("deprecation")
    public void setIt(Bitmap result){
        image = new BitmapDrawable(result);
        if (image != null) {
            try{Thread.sleep(2000);}catch(Exception e){}
            placeholder.setVisibility(View.GONE);
            setImageDrawable(image);
        }
    }
}

Here is my CustomAdapter .

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.smartsync.ImageCache;
import com.smartsync.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Toast;

public class ImageAdapter extends ArrayAdapter<URL> {
    private Activity activity;
    private int layoutResourceId;
    private ArrayList<URL> data = new ArrayList<URL>();
    ExecutorService exec;
    private ImageCache cache;

    public ImageAdapter(Context context, int layoutResourceId, ArrayList<URL> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.activity = (Activity)context;
        exec = Executors.newSingleThreadExecutor();
        this.data = data;
        this.cache = new ImageCache(context);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View row = convertView;
        final URL url = data.get(position);
        final WebImageView image;

        if (row == null) {
            row = activity.getLayoutInflater().inflate(layoutResourceId, parent, false);
            image = (WebImageView) row.findViewById(R.id.webimage);
            image.setPlaceholderImage(R.id.pbar);
            this.notifyDataSetChanged();
            row.setTag(image);
        }
        else
            image = (WebImageView) row.getTag();

        final ImageAdapter adapter = this;
        /*if(position == 0){
            row.setVisibility(View.GONE);
            return row;
        }*/
        exec.execute(new Runnable(){            
            @Override
            public void run() {
                try {
                    Bitmap bitmap = null;
                    bitmap = cache.getBitmap(url);
                    if(bitmap == null){
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inPurgeable = true;
                        options.outHeight = 100;
                        options.outWidth = 100;
                        options.inSampleSize = 5;
                        bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream(), null, options);
                        cache.setCache(bitmap, url);
                    }
                    final Bitmap bmp = bitmap;
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            image.setIt(bmp);
                            adapter.notifyDataSetChanged();
                        }
                    });
                } catch (Exception e) {
                    Log.i("test", ""+e);
                }   
            }
        });

        return row;
    }

}

And this is layout for a single row.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:orientation="vertical"
    android:padding="5dp" 
    android:clickable="true"
    android:focusable="true">

    <com.smartsync.adapters.WebImageView
        android:id="@+id/webimage"
        android:background="@xml/border"
        android:cropToPadding="true"
        android:scaleType="centerCrop"
        android:padding="3dip"
        android:adjustViewBounds="true"
        android:layout_width="100dp"
        android:layout_height="100dp" />

    <ProgressBar 
       android:id="@+id/pbar"
       style="?android:attr/progressBarStyleLarge"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerInParent="true" />

</RelativeLayout>

Please tell me how to solve this problem. I have searched every question regarding this position problem but couldn't solve this.

I have found the reason myself again. The issue was I was calling this.notifyDataSetChanged(); in getView() which was causing to draw that again and hence starts loading itself causing issue. I have just removed this line. I haven't need this because this line is used when we change number of data sets but in my case those were loaded already. Removing progress bar and loading image was not the task of this line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM