选项卡切换后TabHost片段中的GridView消失

时间:2012-06-06 14:23:43

标签: android tabs android-fragments

我有三个标签,一张地图,一个图片库和一个设置标签。我尝试使用片段进行设置,但它们大部分都有效。为了让地图标签正常工作,我跟踪了这​​个家伙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) {}
}

任何帮助,非常感谢

1 个答案:

答案 0 :(得分:1)

万一有人好奇,我解决了。问题是我没有在onCreateView中调用gridView.setAdapter。一切都是第一次工作的原因是因为那是光标加载的时候,所以它在onLoadFinished中调用,如上所示。

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