0

I have an android application which has a grid view gallery in it. On this grid view I'm showing images in full screen when the user clicks on an image. It works quite well. But the problem is grid view shows the images correctly when they appear on the screen in the first time, but the order is changing randomly when user scrolls it down to see more images. Here is my code below for adapters and you can take a look to the pictures.

    public class GridGallery extends BaseAdapter {

    Context context;

    public GridGallery(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return urlList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        System.gc();

        ImageView view;

        if(convertView == null) {

            view = new ImageView(context);

            view.setScaleType(ImageView.ScaleType.FIT_CENTER);
            view.setLayoutParams(new GridView.LayoutParams(screenWidth/4, screenHeight/4));
            view.setAdjustViewBounds(false);
            view.setPadding(2, 2, 2, 2);

            imageLoader.DisplayImage(urlList.get(position), view);

            System.gc();

        }else {
            view = (ImageView) convertView;
        }

        System.gc();

        return view;
    }

}

And here how I set the adapter below,

GridView g = (GridView) findViewById(R.id.myGrid);
        g.setAdapter(new GridGallery(context));

        isGalleryVisible = false;

        g.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

                myPagerAdapter = new MyPagerAdapter(context);
                pager = (ViewPager) findViewById(R.id.gallery_view);
                pager.setAdapter(myPagerAdapter);
                pager.setCurrentItem(position);

                isGalleryVisible = true;

            }
        });

Here are my screen shots, It repeats itself when I scroll it. And every time I scroll down and go back to top their places are changing randomly.

Any help will appreciated. Thanks in advance.

osayilgan
  • 5,873
  • 7
  • 47
  • 68

2 Answers2

2

Yeah, I just faced the same problem right now. I am also using Fedor's lazy loading concept.I am not sure how far this will be helpful and whether this is the right approach. But still it solved the problem for me.

I had to do a little modification in the getView(),

  @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        System.gc();

        ImageView view;

        if(convertView == null) {

            view = new ImageView(context);

            view.setScaleType(ImageView.ScaleType.FIT_CENTER);
            view.setLayoutParams(new GridView.LayoutParams(screenWidth/4, screenHeight/4));
            view.setAdjustViewBounds(false);
            view.setPadding(2, 2, 2, 2);



            System.gc();

        }else {
            view = (ImageView) convertView;
        }
          if(view!=null)
            {
                   imageLoader.DisplayImage(urlList.get(position), view);
                   notifyDataSetChanged();  //Calling this helped to solve the problem. 
            }

        System.gc();

        return view;
    }
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • Thanks man, Accepted your answer for your solution. I would like to ask something else related to this one, this grid view is quite slow, is yours the same ? do you know how can I make it faster ? – osayilgan May 23 '12 at 10:45
  • I am not sure. But I faced an issue when I tried to use huge resolution images. Try scaling it. BCoz grid is gonna be of small width and height anyways, why to use huge images. – Andro Selva May 23 '12 at 10:50
  • 2
    I'm using small images actually, and I found the problem. It was because of garbage collection. since Fedor's lazy loading is using weak references, calling System.gc() is killing memory cache. That's why Adapter is trying to create them every single time I scroll. – osayilgan May 23 '12 at 11:35
  • But now I have another issue, I tried your method and it works quite well. But the thing is If scroll down once completely and when I scroll up, my click on Item event is not working. do have any idea ? – osayilgan May 23 '12 at 11:37
0

I have just deleted the

if(convertView==null) it works