3

How can I skip some cells in grid view. I mean I have next source:

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    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) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.sample_2, R.drawable.sample_3, etc...
}

main:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));

and xml:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
/>

Now I have this:
enter image description here

How can I skip some cells in my grid view, I want something like this: enter image description here

AndroidLearner
  • 4,500
  • 4
  • 31
  • 62
Laser
  • 6,652
  • 8
  • 54
  • 85

1 Answers1

2

try the following:

EDIT

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private boolean[] arrGrid;

    public ImageAdapter(Context c) {
        mContext = c;

        arrGrid = new boolean[mThumbIds.length];
        for (int i = 0; i < arrGrid.length; i++) {
            arrGrid[i] = true;
        }

        // set the following positions blank for example
        arrGrid[4] = false; 
        arrGrid[5] = false; 
        arrGrid[8] = false; 
    }

    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) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            if (arrGrid[position]) {
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);                    
                imageView.setImageResource(mThumbIds[position]);                     
            } 
        } else {
            imageView = (ImageView) convertView;
        }

        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.sample_2, R.drawable.sample_3, etc...
    }
}
mango
  • 5,577
  • 4
  • 29
  • 41
  • It's helped only to don't `setImageResource`. But creation of `Imageview` still anyway. Do you know how i can workaround it? – Laser Dec 04 '12 at 11:09
  • That's pretty good, but I see the potential for imageView to be undefined; I'm surprised the compiler didn't flag it. Also bear in mind that convertView is used to recycle existing views, but there's no reason to think it will be used to recycle a view in the same position as before. This means that even in the case where convertView is non-null, you still need to check arrGrid[position] to see if convertView should be returned or replaced with a null view. – Edward Falk Dec 05 '12 at 08:34
  • @EdwardFalk some eye-opening points there. in the interest of my own improvement, how about this? http://pastebin.com/9NaGMvKP or perhaps the final should be `convertView = imageView;` – mango Dec 05 '12 at 08:47
  • Well, to start with, I don't even know if it's legal for getView() to return null, but if so, that's ideal. If not, then you should be returning an empty 1x1 View object, or a Space view (API 14+). What you've linked to won't compile, but you're on the right track. I would do "if (!arrGrid[position]) return null; if (convertView != null) { // recycle convertview } else { // create new view } – Edward Falk Dec 05 '12 at 09:14
  • @EdwardFalk Also i found intresting results on widescreen panels if i use fixed `GridView` with 4 colums and add 6 rows with `ImageView` by this method. On my phone they can not be displayed at one screen so i use finger for list down, but if i do it fast `gridView` destroys, i will post it on youtube, asap. So it's doesnt solved my problem. – Laser Dec 06 '12 at 18:25
  • @Pepelac I think the best way would be to make a blank view and insert that depending on the positions of the array. – mango Dec 07 '12 at 07:54
  • Crashes with NPE on API-21 and Android 4.4.4 test device. Fixed via making required View INVISIBLE. And don't forget to use ViewHolder pattern. – Sergii Nov 28 '14 at 09:13