我的代码生成的gridview只有水平间距但没有垂直间距。即使在搜索谷歌后也找不到问题。如果我向imageview添加填充,我只获得垂直间距,但它没有给我所需的间距。这是我的代码,我使用通用图像加载器:
public PhotoGridFragment() {}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Get the individual image size in the grid and the grid image spacing
mImageThumbSize = getResources().getDimensionPixelSize(R.dimen.image_thumbnail_size);
mImageThumbSpacing = getResources().getDimensionPixelSize(R.dimen.image_thumbnail_spacing);
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.empty_photo)
.showImageForEmptyUri(R.drawable.empty_photo)
.showImageOnFail(R.drawable.empty_photo)
.cacheInMemory(true)
.cacheOnDisk(true)
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
mAdapter = new ImageAdapter(getActivity());
if(((MainActivity)getActivity()).isRunningFirstTime) {
}
}
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.photo_grid_fragment, container, false);
final GridView mGridView = (GridView) v.findViewById(R.id.gridview);
mGridView.setAdapter(mAdapter);
mGridView.setOnItemClickListener(this);
mGridView.setOnScrollListener(new PauseOnScrollListener(ImageLoader.getInstance(), false, true));
// This listener is used to get the final width of the GridView and then calculate the
// number of columns and the width of each column. The width of each column is variable
// as the GridView has stretchMode=columnWidth. The column width is used to set the height
// of each view so we get nice square thumbnails.
mGridView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@SuppressWarnings("deprecation")
@TargetApi(VERSION_CODES.JELLY_BEAN)
@Override
public void onGlobalLayout() {
if (mAdapter.getNumColumns() == 0) {
final int numColumns = (int) Math.floor(
mGridView.getWidth() / (mImageThumbSize + mImageThumbSpacing));
if (numColumns > 0) {
final int columnWidth =
(mGridView.getWidth() / numColumns) - mImageThumbSpacing;
columnNum = numColumns;
mAdapter.setNumColumns(numColumns);
mAdapter.setItemHeight(columnWidth);
if (Utils.hasJellyBean()) {
mGridView.getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
} else {
mGridView.getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
}
}
}
}
});
return v;
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@TargetApi(VERSION_CODES.JELLY_BEAN)
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
final Intent i = new Intent(getActivity(), PhotoDetailActivity.class);
i.putExtra(PhotoDetailActivity.EXTRA_IMAGE, (photoIds[position - columnNum]));
if (Utils.hasJellyBean()) {
// makeThumbnailScaleUpAnimation() looks kind of ugly here as the loading spinner may
// show plus the thumbnail image in GridView is cropped. so using
// makeScaleUpAnimation() instead.
ActivityOptions options =
ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getWidth(), v.getHeight());
getActivity().startActivity(i, options.toBundle());
} else {
startActivity(i);
}
}
public int getCurTab() {
return getArguments().getInt(CURRENT_TAB);
}
public static PhotoGridFragment newInstance(int position, int curTab) {
PhotoGridFragment photoGrid = new PhotoGridFragment();
Bundle categoryId = new Bundle();
categoryId.putInt(CATEGORY_ID, position);
categoryId.putInt(CURRENT_TAB, curTab);
photoGrid.setArguments(categoryId);
return photoGrid;
}
/**
* 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 GridView.LayoutParams mImageViewLayoutParams;
public ImageAdapter(Context context) {
super();
mContext = context;
mImageViewLayoutParams = new GridView.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
}
@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 photoIds.length;
}
@Override
public Object getItem(int position) {
return photoIds[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return 1;
}
@Override
public View getView(int position, View convertView, ViewGroup container) {
// Now handle the main ImageView thumbnails
ImageView imageView;
if (convertView == null) { // if it's not recycled, instantiate and initialize
imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(mImageViewLayoutParams);
} else { // Otherwise re-use the converted view
imageView = (ImageView) convertView;
}
if ((imageView.getLayoutParams().height != mItemHeight)
|| (imageView.getLayoutParams().width != 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
ImageLoader.getInstance()
.displayImage(photoIds[position], imageView, options);
return imageView;
//END_INCLUDE(load_gridview_item)
}
/**
* 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(mItemHeight, mItemHeight);
notifyDataSetChanged();
}
public void setNumColumns(int numColumns) {
mNumColumns = numColumns;
}
public int getNumColumns() {
return mNumColumns;
}
}
}
Gridview xml:
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_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"
android:drawSelectorOnTop="true"
android:listSelector="@drawable/photogrid_list_selector" />
答案 0 :(得分:1)
花了6个小时试图找到问题。现在我有了解决方案。问题是ldpi设备上没有显示1dp间距,但显示在mdpi,xhdpi等上。我刚刚创建了一个“values-ldpi”文件夹并创建了一个dimens.xml文件夹,然后添加了维度image_thumbnail_spacing并将其分配给2dp和它现在正在工作。感谢