0

我有三个选项卡、一个地图、一个图片库和一个设置标签。我尝试使用片段来设置它们,并且它们在大多数情况下都有效。为了让地图选项卡正常工作,我在 Fragment (Honeycomb) 中关注了这个人的东西 MapView

设置选项卡使用 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) {}
}

任何帮助,非常感谢

4

1 回答 1

1

如果有人好奇,我解决了它。问题是我没有在 onCreateView 中调用 gridView.setAdapter。第一次一切正常的原因是因为那是光标被加载的时候,所以它在 onLoadFinished 中被调用,正如你在上面看到的。

我需要把它放在 onCreateView 因为我的 gridview 每次都在那里重新定义,所以如果我不调用 setAdapter 并且光标已经加载。图片没有开始填充。

于 2012-06-06T18:57:34.033 回答