2012-12-04 140 views
3

如何跳過網格視圖中的某些單元格。 我的意思是我有一個源:Android gridview跳過單元格

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... 
} 

主:

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

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

和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" 
/> 

現在我有這樣的:
enter image description here

我怎麼能跳過我的網格視圖中的一些單元格,我想要一些像這樣的事情: enter image description here

回答

2

嘗試以下操作:

編輯

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... 
    } 
} 
+0

這只是幫着做不'setImageResource'。但無論如何,仍然會創建'Imageview'。你知道我能如何解決它嗎? – Arseniy

+0

我編輯我的帖子,嘗試 – mango

+0

這是謝謝。 – Arseniy