Images in gridview get replaced on scrolling

亡梦爱人 提交于 2019-12-13 07:18:52

问题


I have coded to display few images in gridview. The images are getting displayed from urls. The problem i am facing is that the images get replaced when i scroll.. and the order of images keeps on changing once started.. this leads to delay in display of full image if i click on any image in grid.. Please help !

code :

public class ImageAdapter extends BaseAdapter {

    private Context mContext;
    private TextView txtUrl;
    private String response;
    public ImageView imageView;
    public static String[] mThumbIds;
    public ImageAdapter(Context c,String resp) {
        mContext = c;
        response = resp.trim();
        mThumbIds = resp.split(",");
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(95, 95));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(6, 6, 6, 6);
        } else {
            imageView = (ImageView) convertView;
        }


        try {
             new LoadImageGrid(imageView).execute(mThumbIds[position]);

        } catch(Exception e) {
            txtUrl.setText("Error: Exception");
        }

        return imageView;

    }



class LoadImageGrid extends AsyncTask<String, Void, Drawable>
{

    ImageView imageView;
    public LoadImageGrid(ImageView im){
        imageView = im;
    }
    @Override
    protected Drawable doInBackground(String... args) {
        String url = args[0];
        Drawable d = null;
        try {
            d = Drawable.createFromStream((InputStream) new URL(url).getContent(), "src");
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return d;

    }


    @Override
    protected void onPostExecute(Drawable d) {
        imageView.setImageDrawable(d);
    }

回答1:


When you scroll your grid view, image views that are not visible returns to getView() as convertView parameter. But your asyncTask doesn't stop, and call

imageView.setImageDrawable(d);

leads to applying downloaded image to the view on wrong position. Because now you reuse the same imageView. Quick fix is not to use convertView. But it'll slow down your app a bit.




回答2:


This seems to be related to convert view. When you reuse the space for an image using convert view then you should use the Imageview within that scope. I would suggest to find the imageview by its id within your current class code and then popultae it.



来源:https://stackoverflow.com/questions/15297645/images-in-gridview-get-replaced-on-scrolling

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