我试图在开源项目之上构建我的应用程序。基本功能是使用 GridView 显示几个图像。以下是代码片段。注释行是原始代码。在原始代码中,Images.imageThumbUrls
是 url 的字符串数组,size( Images.imageThumbUrls.length
) 为 99。我想用我自己的版本替换原始 url,我做到了。但是有一个错误。请看日志。
根据日志,似乎限制为getView
99(原始代码)。除了改变getCount
,我还有什么需要做的吗?
在createView
方法:
mGridView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@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;
mAdapter.setNumColumns(numColumns);
mAdapter.setItemHeight(columnWidth);
if (BuildConfig.DEBUG) {
Log.d(TAG, "onCreateView - numColumns set to " + numColumns);
}
}
}
}
});
图像适配器类:
private class ImageAdapter extends BaseAdapter {
public int getCount() {
//return Images.imageThumbUrls.length + mNumColumns;
return DataStore.photosInfoOfTag.total + mNumColumns;
}
@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));
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
Photo photo = DataStore.photosInfoOfTag.photo.get(position - mNumColumns);
String photoUrl = DataStore.imageUrlPart1 + Integer.toString(photo.getFarm()) +
DataStore.imageUrlPart2 + photo.getServer() + "/" +
photo.getId() + "_" + photo.getSecret() +
DataStore.imageUrlPart3;
mImageFetcher.loadImage(photoUrl, imageView);
//mImageFetcher.loadImage(Images.imageThumbUrls[position - mNumColumns], imageView);
return imageView;
}
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;
}
}
05-06 01:04:39.736:E/AndroidRuntime(16356):致命异常:主要
05-06 01:04:39.736: E/AndroidRuntime(16356):
java.lang.IndexOutOfBoundsException: 无效索引 100,大小为 10005-06 01:04:39.736: E/AndroidRuntime(16356): 在 java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
05-06 01:04:39.736: E/AndroidRuntime(16356): 在 java.util.ArrayList.get(ArrayList.java:304)
05-06 01:04:39.736: E/AndroidRuntime(16356): 在 com.example.android.bitmapfun.ui.ImageGridFragment$ImageAdapter.getView(ImageGridFragment.java:292)
05-06 01:04:39.736: E/AndroidRuntime(16356): 在 android.widget.AbsListView.obtainView(AbsListView.java:2143)
05-06 01:04:39.736: E/AndroidRuntime(16356): 在 android.widget.GridView.makeAndAddView(GridView.java:1341)
05-06 01:04:39.736: E/AndroidRuntime(16356): 在 android.widget.GridView.makeRow(GridView.java:341)
05-06 01:04:39.736: E/AndroidRuntime(16356): 在 android.widget.GridView.fillDown(GridView.java:283)
05-06 01:04:39.736: E/AndroidRuntime(16356): 在 android.widget.GridView.fillGap(GridView.java:243)
05-06 01:04:39.736: E/AndroidRuntime(16356): 在 android.widget.AbsListView.trackMotionScroll(AbsListView.java:4930)
05-06 01:04:39.736: E/AndroidRuntime(16356): 在 android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4087)
05-06 01:04:39.736: E/AndroidRuntime(16356): 在 android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
05-06 01:04:39.736: E/AndroidRuntime(16356): 在 android.view.Choreographer.doCallbacks(Choreographer.java:562)
05-06 01:04:39.736: E/AndroidRuntime(16356): 在 android.view.Choreographer.doFrame(Choreographer.java:531)
05-06 01:04:39.736: E/AndroidRuntime(16356): 在 android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
05-06 01:04:39.736: E/AndroidRuntime(16356): 在 android.os.Handler.handleCallback(Handler.java:725)
05-06 01:04:39.736: E/AndroidRuntime(16356): 在 android.os.Handler.dispatchMessage(Handler.java:92)
05-06 01:04:39.736: E/AndroidRuntime(16356): 在 android.os.Looper.loop(Looper.java:137)
05-06 01:04:39.736: E/AndroidRuntime(16356): 在 android.app.ActivityThread.main(ActivityThread.java:5041)
05-06 01:04:39.736: E/AndroidRuntime(16356): 在 java.lang.reflect.Method.invokeNative(Native Method)
05-06 01:04:39.736: E/AndroidRuntime(16356): 在 java.lang.reflect.Method.invoke(Method.java:511)
05-06 01:04:39.736: E/AndroidRuntime(16356): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-06 01:04:39.736: E/AndroidRuntime(16356): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-06 01:04:39.736: E/AndroidRuntime(16356): at dalvik.system.NativeStart.main(Native Method)