0

我有一个Activity由两个组成的布局Fragment。其中一个片段布局包含一个GridView.

public class PicturesGallery extends FragmentActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gallery_frames);

        Fragment galleryFragment = fm.findFragmentById(R.id.gallery_fragment);
        if (galleryFragment == null) {
           fm.beginTransaction().add(R.id.gallery_fragment, new GalleryFragment()).commit();
        }
        ...

当这个片段被调用时,它会实例化 的适配器GridView,膨胀GridView并设置适配器(它已经被提供了ArrayList15 个要显示的项目)。

public class GalleryFragment extends Fragment {
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // Initialize the layout Adapter
        PicturesGallery pga = (PicturesGallery) getActivity();
        mAdapter = new ImageAdapter(pga.getApplication(), pga.mMemoryCache,
                galleryDir);
        mAdapter.mPictureNames = new ArrayList<String>(pictureNames);

        // Inflate the layout and set the adapter
        LayoutInflater inflater = (LayoutInflater) getActivity()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        GridView gridView = (GridView) inflater.inflate(R.layout.pictures_gallery_fragment, null);
        gridView.setAdapter(mAdapter);
    }
    ....

但是当活动被执行时,什么也没有发生(logcat 中没有错误,但没有显示任何内容GridView)。在适配器类中,使用Log.d()insidegetView()表示从不调用该方法。但是getCount()返回正确数量的项目。相关代码如下。

public class ImageAdapter extends BaseAdapter {
    ...
    public ArrayList<String> mPictureNames = new ArrayList<String>();
    public ImageAdapter(Context c, LruCache<String, Bitmap> mCache, File gallery) {
        mMemoryCache = mCache;
        mGalleryDir = gallery;
        mInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        Log.d("dupa", "getView");
        ViewHolder vh;
        View cell = convertView;
        if (cell == null) {
            vh = new ViewHolder();
            cell = mInflater.inflate(R.layout.galleryitem, null);
            // Populate the ViewHolder
            vh.checkBox = (CheckBox) cell.findViewById(R.id.itemCheckBox);
            ...
            cell.setTag(vh);
            ...
        } else {
            vh = (ViewHolder) cell.getTag();
        }
        // Update the cell View state
        vh.checkBox.setTag(position);
        ...
        return cell;
    }

我没有包含 xml 文件,因为帖子很长。如果您认为需要它们(或者您需要更多代码),请告诉我。任何帮助将不胜感激。TIA。

4

1 回答 1

1

您需要onCreateView()在您的片段中实现并在那里返回膨胀布局(注意使用inflate不将膨胀视图添加到容器的方法。http ://developer.android.com/reference/android/app/Fragment.html#onCreateView (android.view.LayoutInflater , android.view.ViewGroup, android.os.Bundle)

就像页面顶部的示例一样

/**
 * The Fragment's UI is just a simple text view showing its
 * instance number.
 */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.hello_world, container, false);
    View tv = v.findViewById(R.id.text);
    ((TextView)tv).setText("Fragment #" + mNum);
    tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
    return v;
}

在那里你可以记住 GridView 的实例,然后在onActivityCreated(或者可能在onAttach()你可以设置适配器)中。

于 2012-09-19T19:35:48.860 回答