1

我的 GridView 有问题。我正在使用 Bitmapfun 项目的一部分,我不想在顶部留出边距。

看到这个捕获: 在此处输入图像描述

我在微调器和第一张图片之间有一个大的黑色区域(我放了 3 个问号)。

如果我向下滚动,我会得到一个很好的渲染:照片在微调器下滑动: 在此处输入图像描述

如果我回到顶部,我会在第一张照片之前再次看到这个大的黑色区域。有人可以帮助我吗?

这是我的网格视图(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>

值/样式.xml:

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

值-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();
        }
    }
}

在开发选项中激活布局边界后,我得到了以下结果:

在此处输入图像描述

4

2 回答 2

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)中设置了错误。

于 2014-02-12T22:38:17.637 回答
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;
    }
}

看中间,在 public View getView() 中:Bitmapfun 项目添加了一个高度等于 ActionBar 高度的空视图(因为在 Bitmapfun 原始项目上可以看到 actionbar)。

如果我们注释这一行(或者如果我们在 public ImageAdapter() 中让 mActionBarHeight 为 0),我们在第一张图片之前没有这个空间。

也感谢 Tim Castelijns .!

于 2014-02-14T23:04:45.700 回答