我有三個選項卡,一個地圖,一個圖片庫和一個設置標籤。我試圖用碎片來設置它們,並且它們大部分都起作用。爲了讓地圖選項卡正常工作,我跟着這個人的東西MapView in a Fragment (Honeycomb)。TabHost中的GridView片段開關後消失片段
設置選項卡使用一個ListFragment並且工作正常,就像地圖一樣。我的圖像標籤也是第一次正確運行。我導航到它,所有的圖像加載正確,但如果我切換到其他選項卡之一,並返回到圖像選項卡,一切都消失了,沒有加載。如果我退出應用程序並再次打開它,圖像選項卡將正確加載,這使我認爲某些內容正在加載正確,但未被重新初始化或當我導航到該選項卡時。
圖片庫代碼:
public class ImageGridFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
ImageAdapter imageAdapter;
GalleryItem[] galleryItems = new GalleryItem[0];
GridView gridView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
getActivity().getSupportLoaderManager().initLoader(0, null, this);
imageAdapter = new ImageAdapter(getActivity(), galleryItems);
}
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.image_grid_fragment, container, false);
gridView = (GridView) v.findViewById(R.id.gridView);
Gallery.setContentResolver(getActivity().getContentResolver());
Gallery.setDefaultBitmap(getResources(), R.drawable.loading);
gridView.setOnItemClickListener(new GridView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, final int position, long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final LayoutInflater
inflater = (LayoutInflater) getActivity().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.image_popup, null);
ImageView image = (ImageView) view.findViewById(R.id.image);
Drawable d = Drawable.createFromPath(galleryItems[position].getUri().getPath());
image.setImageDrawable(d);
image.setMinimumHeight(d.getMinimumHeight());// (int) (d.getIntrinsicHeight() * 2.5));
image.setMinimumWidth(d.getIntrinsicWidth());// (int) (d.getIntrinsicWidth() * 2.5));
builder.setPositiveButton("Send", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent sendIntent = new Intent(getActivity(), RecordSomething.class);
sendIntent.putExtra("FILE", new File(galleryItems[position].getUri().getPath()));
startActivityForResult(sendIntent, Constants.ACTIVITY_SEND_PHOTO);
}
});
builder.setNegativeButton("Cancel", null);
builder.setView(view);
builder.show();
}
});
return v;
}
/* Creates the menu items */
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.gallery_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
/* Handles item selections */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_camera:
Intent camIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
/*
* The below code was used to send a parameter to the native camera app to tell it
* to save a copy of the image taken to the file we specified. This worked fine,
* except that the image lost it's orientation. What we do instead is in onActivityResult
* we find the most recent image taken, move it to our folder, rename it, and resize it
*/
startActivityForResult(camIntent, Constants.ACTIVITY_TAKE_PHOTO);
return true;
}
return false;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case Constants.ACTIVITY_TAKE_PHOTO:
if (resultCode == Activity.RESULT_OK) {
Intent sendIntent = new Intent(getActivity(), RecordSomething.class);
sendIntent.putExtra("FILE", "test"); //TODO get the path from the array of pictures
startActivityForResult(sendIntent, Constants.ACTIVITY_SEND_PHOTO);
}
break;
}
}
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
return new CursorLoader(getActivity(),
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.ORIENTATION,
MediaStore.Images.Media.DATA
},
MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME + " = 'Camera'",
null,
MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
}
@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
galleryItems = new GalleryItem[cursor.getCount()];
cursor.moveToFirst();
if (cursor.moveToFirst()) {
int i = 0;
do {
galleryItems[i] = new GalleryItem(
cursor.getInt(cursor.getColumnIndex(MediaStore.Images.ImageColumns._ID)),
cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)),
cursor.getInt(cursor.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION)));
i++;
} while (cursor.moveToNext());
}
imageAdapter.setGalleryItems(galleryItems);
gridView.setAdapter(imageAdapter);
}
@Override
public void onLoaderReset(Loader<Cursor> cursorLoader) {}
}
任何幫助,非常感謝
notifydatasetchanged works also also – Josh