2014-02-12 134 views
1

我的GridView出現問題。我正在使用Bitmapfun項目的一部分,我不想在頂部留有餘量。GridView上的邊距/填充頂部

看到這個捕獲: enter image description here

我有一個大的黑色區域(這裏我把3個問號)的微調和第一畫面之間。

如果我向下滾動,我得到一個很好的渲染:照片微調下滑動: enter image description here

如果我回去的頂部,我再有這樣的大黑區僅第一圖片之前。 有人可以幫助我嗎?

這是我的GridView(image_grid_fragment.xml):

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridView" 
    style="@style/PhotoGridLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:columnWidth="@dimen/image_thumbnail_size" 
    android:horizontalSpacing="@dimen/image_thumbnail_spacing" 
    android:numColumns="auto_fit" 
    android:stretchMode="columnWidth" 
    android:verticalSpacing="@dimen/image_thumbnail_spacing" > 

</GridView> 

GridView控件被包括在該主佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/TextView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="17dp" 
     android:layout_marginLeft="16dp" 
     android:text="Album : " 
     android:textAppearance="?android:attr/textAppearanceSmallPopupMenu" 
     android:textSize="18dp" /> 

    <Spinner 
     android:id="@+id/spinner_album" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_toRightOf="@+id/TextView1" 
     android:layout_marginTop="12dp" 
     android:inputType="text" 
     android:textSize="18dp" /> 

    <include layout="@layout/image_grid_fragment" /> 

</RelativeLayout> 

值/ styles.xml:

<style name="PhotoGridLayout"> 
    <item name="android:drawSelectorOnTop">false</item> 
    <item name="android:listSelector">@drawable/photogrid_list_selector</item> 
</style> 

values-v11/styles.xml:

<style name="PhotoGridLayout"> 
    <item name="android:drawSelectorOnTop">true</item> 
</style> 

ImageGridActivity.java:

public class ImageGridActivity extends FragmentActivity { 
    private static final String TAG = "ImageGridFragment"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     if (getSupportFragmentManager().findFragmentByTag(TAG) == null) { 
      final FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
      ft.add(android.R.id.content, new ImageGridFragment(), TAG); 
      ft.commit(); 
     } 
    } 
} 

隨着開發選項激活佈局界限,我有這樣的結果:

enter image description here

+0

你能張貼'風格/ PhotoGridLayout'以及 –

+0

添>當然,我已經編輯我的職務的XML。謝謝 ! – Jerry

+0

好吧,沒有什麼奇怪的..問題可能是什麼看起來像一個微調,超過實際的GridView。你有代碼/ XML的呢? –

回答

0

感謝羅傑異形!他讓我走上了正確的道路!

的問題是在GridView適配器上:ImageGridFragment.java的

部分源代碼:

/** 
* The main adapter that backs the GridView. This is fairly standard except the number of 
* columns in the GridView is used to create a fake top row of empty views as we use a 
* transparent ActionBar and don't want the real top row of images to start off covered by it. 
*/ 
private class ImageAdapter extends BaseAdapter { 

    private final Context mContext; 
    private int mItemHeight = 0; 
    private int mNumColumns = 0; 
    private int mActionBarHeight = 0; 
    private GridView.LayoutParams mImageViewLayoutParams; 

    public ImageAdapter(Context context) { 
     super(); 
     mContext = context; 
     mImageViewLayoutParams = new GridView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
     // Calculate ActionBar height 
     TypedValue tv = new TypedValue(); 
     if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) { 
      mActionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics()); 
     } 
    } 

    @Override 
    public int getCount() { 
     // If columns have yet to be determined, return no items 
     if (getNumColumns() == 0) { 
      return 0; 
     } 

     // Size + number of columns for top empty row 
     return Images.imageThumbUrls.length + mNumColumns; 
    } 

    @Override 
    public Object getItem(int position) { 
     return position < mNumColumns ? 
       null : Images.imageThumbUrls[position - mNumColumns]; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position < mNumColumns ? 0 : position - mNumColumns; 
    } 

    @Override 
    public int getViewTypeCount() { 
     // Two types of views, the normal ImageView and the top row of empty views 
     return 2; 
    } 

    @Override 
    public int getItemViewType(int position) { 
     return (position < mNumColumns) ? 1 : 0; 
    } 

    @Override 
    public boolean hasStableIds() { 
     return true; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup container) { 
     // First check if this is the top row 
     if (position < mNumColumns) { 
      if (convertView == null) { 
       convertView = new View(mContext); 
      } 
      // Set empty view with height of ActionBar 
      convertView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mActionBarHeight)); 
      // 
      // THE PROBLEM IS HERE !! 
      // 

      return convertView; 
     } 

     // Now handle the main ImageView thumbnails 
     ImageView imageView; 
     if (convertView == null) { // if it's not recycled, instantiate and initialize 
      imageView = new RecyclingImageView(mContext); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageView.setLayoutParams(mImageViewLayoutParams); 
     } else { // Otherwise re-use the converted view 
      imageView = (ImageView) convertView; 
     } 

     // Check the height matches our calculated column width 
     if (imageView.getLayoutParams().height != mItemHeight) { 
      imageView.setLayoutParams(mImageViewLayoutParams); 
     } 

     // Finally load the image asynchronously into the ImageView, this also takes care of 
     // setting a placeholder image while the background thread runs 
     mImageFetcher.loadImage(Images.imageThumbUrls[position - mNumColumns], imageView, false); 
     return imageView; 
    } 

    /** 
    * Sets the item height. Useful for when we know the column width so the height can be set 
    * to match. 
    * 
    * @param height 
    */ 
    public void setItemHeight(int height) { 
     if (height == mItemHeight) { 
      return; 
     } 
     mItemHeight = height; 
     mImageViewLayoutParams = new GridView.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight); 
     mImageFetcher.setImageSize(height); 
     notifyDataSetChanged(); 
    } 

    public void setNumColumns(int numColumns) { 
     mNumColumns = numColumns; 
    } 

    public int getNumColumns() { 
     return mNumColumns; 
    } 
} 

看在中間,在公衆視野getView():Bitmapfun項目增加了一個空的觀點高度等於ActionBar的高度(因爲操作欄在Bitmapfun原始項目中可見)。

如果我們註釋此行(或如果我們讓mActionBarHeight 0公共ImageAdapter()),我們沒有前第一張照片這個空間。

感謝Tim Castelijns過!

1

什麼是@layout/image_grid_fragment的內容?

,如果你試圖改變

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridView" 
    style="@style/PhotoGridLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    ... 

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridView" 
    style="@style/PhotoGridLayout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    ... 

在Android 4.2和系統設置,開發人員選項運行你的應用程序是什麼 - >顯示佈局邊界(的地方標記)。你會看到,如果它是一些填充或一些意見的餘量。

此外,您可能會在gridView適配器的bindView(或getView)中設置錯誤。

+0

^這是你的答案'@佈局/ image_grid_fragment' –

+0

謝謝!所以..用「wrap_content」而不是「fill_parent」,我有同樣的結果。 我添加了一個激活佈局界限捕捉:這是奇怪的,結果是奇怪! – Jerry

+0

謝謝!你把我放在正確的路上,我發現了這個問題(下面的答案)。 – Jerry