我有一个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
并设置适配器(它已经被提供了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
)。在适配器类中,使用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。