0

私はActivity2つのレイアウトでを持っていますFragment。フラグメントレイアウトの1つに。が含まれてい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()insideを使用getView()すると、メソッドが呼び出されないことを示します。ただし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 に答える