2012-09-19 89 views
0

我有一個Activity的佈局由兩個Fragment組成。其中一個片段佈局包含GridViewGridView裏面的片段:getView永遠不會被調用

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和設置在適配器(這已被饋送有一個ArrayList 15項被顯示)。

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顯示沒有任何錯誤)。在適配器類中,在getView()內使用Log.d()表示該方法從不調用。但是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。

回答

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()可以設置適配器。

+0

謝謝。它工作正常。當視圖有適配器時,我對使用onCreateView vs onActivityCreated感到困惑。 – Vicent

+0

完美,爲我的gridview工作 – Manny265

相關問題