マップ、画像ギャラリー、設定タグの 3 つのタブがあります。フラグメントを使用してそれらをセットアップしようとしましたが、ほとんどの場合機能します。マップタブを機能させるために、私はこの男のものMapView in a Fragment (Honeycomb) に従いました。
設定タブは 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) {}
}
どんな助けでも大歓迎です