我面临在方向更改时保留网格视图状态的问题。最初,当创建网格视图时,只加载了 9 个图像。当用户滚动网格视图时,更多图像会添加到网格视图中,这非常有效。单击网格视图上的图像时,将打开另一个活动,该活动在图像视图上显示图像,现在,当我返回网格活动时按下后退按钮,网格视图失去了它的状态,这意味着,现在活动开始再次从头开始,仅显示加载的前 9 张图像。
使用通用图像下载器库下载图像。
我已经
android:configChanges="keyboardHidden|orientation"
在清单文件中指定了。覆盖
onConfigurationChanged
andonRetainNonConfigurationInstance
。网格活动:
调试后,我发现在退出活动时调用了 onDestroy() 方法,而不仅仅是 onPause()。我似乎真的无法找出这里似乎是什么问题。任何帮助,将不胜感激。
谢谢。
/** The initial activity class which instantiates a gallery */
public class GalleryActivity extends Activity {
private GridView gridview;
private LinearLayout linlaProgressBar;
private int count = 9;
private GridImageAdapter gridImageAdapter;
private boolean loadMore = false;
private Vector<Image> imageDataList;
private Context context;
private int width = 0;
private int height = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("ACTIVITY ", "ON CREATE");
setContentView(R.layout.grid_layout);
linlaProgressBar = (LinearLayout) findViewById(R.id.linlaProgressBar);
gridview = (GridView) findViewById(R.id.gridview);
gridview.setPadding(10, 10, 10, 10);
imageDataList = new Vector<Image>(100);
setImageData();
context = this;
WindowManager winManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
int screenWidth = winManager.getDefaultDisplay().getWidth();
int screenHeight = winManager.getDefaultDisplay().getHeight();
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
width = (int) (screenWidth * 0.45);
height = (int) (screenHeight * 0.3);
} else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
width = (int) (screenWidth * 0.25);
height = (int) (screenHeight * 0.40);
}
// Check if old activity exist
GalleryActivity oldActivity = (GalleryActivity) getLastNonConfigurationInstance();
if (oldActivity == null) {
gridImageAdapter = new GridImageAdapter(this);
/* set the urls from which the image is to be loaded */
gridImageAdapter.addURL(imageDataList);
/* count is the initial number of images to be loaded */
gridImageAdapter.AddItems(count);
gridview.setColumnWidth(width);
gridImageAdapter.setDimensions(width, height);
/* set the adapter to the grid view */
gridview.setAdapter(gridImageAdapter);
} else {
gridImageAdapter = oldActivity.gridImageAdapter;
gridview.setColumnWidth(width);
gridImageAdapter.setDimensions(width, height);
gridview.setAdapter(gridImageAdapter);
}
linlaProgressBar.setVisibility(View.GONE);
gridview.setOnScrollListener(new OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
loadMore = true;
if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
linlaProgressBar.setVisibility(View.GONE);
}
}
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
int lastInScreen = firstVisibleItem + visibleItemCount;
GridImageAdapter gridAdaptr = (GridImageAdapter) gridview
.getAdapter();
int maxLimit = gridAdaptr.getCount();
if (maxLimit >= (imageDataList.size())) {
loadMore = false;
linlaProgressBar.setVisibility(View.GONE);
} else if (lastInScreen == totalItemCount && (loadMore)) {
linlaProgressBar.setVisibility(View.VISIBLE);
/*
* To check that the number of items added never exceeds the
* total images count
*/
int threshHold = maxLimit + visibleItemCount;
if ((threshHold) > imageDataList.size()) {
int newVisibleItemCount = visibleItemCount
- (threshHold - imageDataList.size());
gridAdaptr.AddItems(newVisibleItemCount);
} else {
gridAdaptr.AddItems(visibleItemCount);
}
gridAdaptr.notifyDataSetChanged();
loadMore = false;
}
}
});
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
Log.i("ITEM", "CLICKED");
Intent intent = new Intent(GalleryActivity.this,
HSMainActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
| Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("itemPosition", position);
intent.putExtra("imageurl", imageDataList.get(position)
.getUrl());
context.startActivity(intent);
}
});
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
@Override
public Object onRetainNonConfigurationInstance() {
Log.i("ACTIVITY", "ON RETAIN CONFIGURATION");
return this;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_gallery, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i("ACTIVITY", "ONDESTROY");
int count = gridview.getCount();
for (int i = 0; i < count; i++) {
final ViewGroup v = (ViewGroup) gridview.getChildAt(i);
if (v != null) {
v.removeAllViews();
}
}
}
@Override
protected void onPause() {
Log.i("ACTIVITY", "on PAUSE");
super.onPause();
}
@Override
protected void onResume() {
Log.i("ACTIVITY", "ON RESUME");
super.onResume();
}
/* get the image data */
public void setImageData() {
ImageData imageData = new ImageData();
imageDataList.clear();
imageDataList = imageData.getImageData();
}
}
网格适配器:
/** Adapter for the GridView class which extends the BaseAdapter */
public class GridImageAdapter extends BaseAdapter {
private Context context;
private LayoutInflater layoutInflater;
private int width = 0;
private int height = 0;
private int textFactor = 35;
private int mCount = 0;
private DisplayImageOptions options;
private Vector<Image> images;
public GridImageAdapter(Context mContext) {
context = mContext;
layoutInflater = LayoutInflater.from(context);
images = new Vector<Image>(100);
/* options for the image loader */
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.loading)
.showImageForEmptyUri(R.drawable.loading)
.showImageOnFail(R.drawable.not_found).cacheInMemory()
.cacheOnDisc().resetViewBeforeLoading()
.bitmapConfig(Bitmap.Config.RGB_565).build();
}
public int getCount() {
return getTotalCount();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = null;
TextView textView = null;
int imagePadding = 10;
if (convertView == null) {
convertView = new View(context);
convertView = layoutInflater.inflate(R.layout.grid_cell_layout,
null);
convertView.setPadding(3, 0, imagePadding - 1, imagePadding);
convertView.setBackgroundResource(R.drawable.background_shadow);
convertView
.setLayoutParams(new GridView.LayoutParams(width, height));
} else {
convertView = (View) convertView;
}
imageView = (ImageView) convertView.findViewById(R.id.image);
// Toast.makeText(context, "Width:" + width + "Height:" + height,
// Toast.LENGTH_SHORT).show();
/** height has to be reduced to display the text */
imageView.setLayoutParams(new LinearLayout.LayoutParams(width, height
- textFactor));
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
/** Universal Image Loader is used to load the images */
textView = (TextView) convertView.findViewById(R.id.title);
textView.setText(images.get(position).getName());
/** Check if the file exists in the disc cache */
if (ImageLoader.getInstance().getDiscCache()
.get(images.get(position).getUrl()).exists()) {
String imageUri = "file://"
+ ImageLoader.getInstance().getDiscCache()
.get(images.get(position).getUrl())
.getAbsolutePath(); // from SD
// card
// Log.i("file path", imageUri);
ImageLoader.getInstance()
.displayImage(imageUri, imageView, options);
// Log.i("Image exists in cache", "!!!");
} else {
ImageLoader.getInstance().displayImage(
images.get(position).getUrl(), imageView, options);
}
textView.setTextColor(Color.BLACK);
return convertView;
}
public int AddItems(int count) {
mCount = mCount + count;
return mCount;
}
public int getTotalCount() {
return mCount;
}
public void addURL(Vector<Image> imageDataList) {
images.clear();
images = imageDataList;
}
public void setDimensions(int screenWidth, int screenHeight) {
width = screenWidth;
height = screenHeight;
}
}
图像数据:
/** 向视图提供图像数据的类 (GridView..) */
public class ImageData {
public Vector<Image> getImageData() {
Vector<Image> imageData = new Vector<Image>(100);
Image imageData0 = new Image();
imageData0.setName("image 0");
imageData0.setUrl("https://lh6.googleusercontent.com/-jZgveEqb6pg/T3R4kXScycI/AAAAAAAAAE0/xQ7CvpfXDzc/s1024/sample_image_01.jpg");
imageData.add(imageData0);
Image imageData1 = new Image();
imageData1.setName("image 1");
imageData1.setUrl("https://lh4.googleusercontent.com/-K2FMuOozxU0/T3R4lRAiBTI/AAAAAAAAAE8/a3Eh9JvnnzI/s1024/sample_image_02.jpg");
imageData.add(imageData1);
Image imageData2 = new Image();
imageData2.setName("image 2");
imageData2.setUrl("https://lh5.googleusercontent.com/-SCS5C646rxM/T3R4l7QB6xI/AAAAAAAAAFE/xLcuVv3CUyA/s1024/sample_image_03.jpg");
imageData.add(imageData2);
Image imageData3 = new Image();
imageData3.setName("image 3");
imageData3.setUrl("https://lh6.googleusercontent.com/-f0NJR6-_Thg/T3R4mNex2wI/AAAAAAAAAFI/45oug4VE8MI/s1024/sample_image_03.jpg");
imageData.add(imageData3);
Image imageData4 = new Image();
imageData4.setName("image 4");
imageData4.setUrl("https://lh3.googleusercontent.com/-n-xcJmiI0pg/T3R4mkSchHI/AAAAAAAAAFU/EoiNNb7kk3A/s1024/sample_image_04.jpg");
imageData.add(imageData4);
Image imageData5 = new Image();
imageData5.setName("image 5");
imageData5.setUrl("https://lh3.googleusercontent.com/-n-xcJmiI0pg/T3R4mkSchHI/AAAAAAAAAFU/EoiNNb7kk3A/s1024/sample_image_05.jpg");
imageData.add(imageData5);
Image imageData6 = new Image();
imageData6.setName("image 6");
imageData6.setUrl("https://lh3.googleusercontent.com/-X43vAudm7f4/T3R4nGSChJI/AAAAAAAAAFk/3bna6D-2EE8/s1024/sample_image_06.jpg");
imageData.add(imageData6);
Image imageData7 = new Image();
imageData7.setName("image 7");
imageData7.setUrl("https://lh5.googleusercontent.com/-MpZneqIyjXU/T3R4nuGO1aI/AAAAAAAAAFg/r09OPjLx1ZY/s1024/sample_image_07.jpg");
imageData.add(imageData7);
Image imageData8 = new Image();
imageData8.setName("image 8");
imageData8.setUrl("https://lh6.googleusercontent.com/-ql3YNfdClJo/T3XvW9apmFI/AAAAAAAAAL4/_6HFDzbahc4/s1024/sample_image_08.jpg");
imageData.add(imageData8);
Image imageData9 = new Image();
imageData9.setName("image 9");
imageData9.setUrl("https://lh5.googleusercontent.com/-Pxa7eqF4cyc/T3R4oasvPEI/AAAAAAAAAF0/-uYDH92h8LA/s1024/sample_image_09.jpg");
imageData.add(imageData9);
Image imageData10 = new Image();
imageData10.setName("image 10");
imageData10.setUrl("https://lh4.googleusercontent.com/-Li-rjhFEuaI/T3R4o-VUl4I/AAAAAAAAAF8/5E5XdMnP1oE/s1024/sample_image_10.jpg");
imageData.add(imageData10);
Image imageData11 = new Image();
imageData11.setName("image 11");
imageData11.setUrl("https://lh5.googleusercontent.com/-_HU4fImgFhA/T3R4pPVIwWI/AAAAAAAAAGA/0RfK_Vkgth4/s1024/sample_image_11.jpg");
imageData.add(imageData11);
Image imageData12 = new Image();
imageData12.setName("image 12");
imageData12.setUrl("https://lh6.googleusercontent.com/-0gnNrVjwa0Y/T3R4peGYJwI/AAAAAAAAAGU/uX_9wvRPM9I/s1024/sample_image_12.jpg");
imageData.add(imageData12);
Image imageData13 = new Image();
imageData13.setName("image 13");
imageData13.setUrl("https://lh3.googleusercontent.com/-HBxuzALS_Zs/T3R4qERykaI/AAAAAAAAAGQ/_qQ16FaZ1q0/s1024/sample_image_13.jpg");
imageData.add(imageData13);
Image imageData14 = new Image();
imageData14.setName("image 14");
imageData14.setUrl("https://lh4.googleusercontent.com/-cKojDrARNjQ/T3R4qfWSGPI/AAAAAAAAAGY/MR5dnbNaPyY/s1024/sample_image_14.jpg");
imageData.add(imageData14);
Image imageData15 = new Image();
imageData15.setName("image 15");
imageData15.setUrl("https://lh3.googleusercontent.com/-WujkdYfcyZ8/T3R4qrIMGUI/AAAAAAAAAGk/277LIdgvnjg/s1024/sample_image_15.jpg");
imageData.add(imageData15);
Image imageData16 = new Image();
imageData16.setName("image 16");
imageData16.setUrl("https://lh6.googleusercontent.com/-FMHR7Vy3PgI/T3R4rOXlEKI/AAAAAAAAAGs/VeXrDNDBkaw/s1024/sample_image_16.jpg");
imageData.add(imageData16);
Image imageData17 = new Image();
imageData17.setName("image 17");
imageData17.setUrl("https://lh4.googleusercontent.com/-mrR0AJyNTH0/T3R4rZs6CuI/AAAAAAAAAG0/UE1wQqCOqLA/s1024/sample_image_17.jpg");
imageData.add(imageData17);
Image imageData18 = new Image();
imageData18.setName("image 18");
imageData18.setUrl("https://lh6.googleusercontent.com/-z77w0eh3cow/T3R4rnLn05I/AAAAAAAAAG4/BaerfWoNucU/s1024/sample_image_18.jpg");
imageData.add(imageData18);
Image imageData19 = new Image();
imageData19.setName("image 19");
imageData19.setUrl("https://lh5.googleusercontent.com/-aWVwh1OU5Bk/T3R4sAWw0yI/AAAAAAAAAHE/4_KAvJttFwA/s1024/sample_image_19.jpg");
imageData.add(imageData19);
Image imageData20 = new Image();
imageData20.setName("image 20");
imageData20.setUrl("https://lh6.googleusercontent.com/-q-js52DMnWQ/T3R4tZhY2sI/AAAAAAAAAHM/A8kjp2Ivdqg/s1024/sample_image_20.jpg");
imageData.add(imageData20);
return imageData;
}
}