我在片段中有gridView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
//
Bundle savedInstanceState) {
if (container == null) {
Log.e("Null container", "Null");
return null;
}
Log.e("fragment1", "fragment1 reached!");
View view = inflater.inflate(R.layout.menu1_fragment, container, false);
GridView gridView = (GridView)view.findViewById(R.id.gridview1);
gridView.setAdapter(new ImageAdapter(this.getActivity()));
Log.e("fragment1", "fragment1 reached!");
return (GridView)inflater.inflate(R.layout.menu1_fragment, container, false);
}
但是适配器不起作用,它没有达到覆盖的 getView 方法。
构造函数:
public ImageAdapter(Context c) {
mContext = c;
Log.e("ImageAdapter", "constructor");
}
获取视图方法:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.e("ImageAdapter", "dsfdsf");
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
if (DefinedValues.width/3 < DefinedValues.height/4)
imageView.setLayoutParams(new GridView.LayoutParams
(DefinedValues.height/4, DefinedValues.height/4));
else
imageView.setLayoutParams(new GridView.LayoutParams
(DefinedValues.width/3-5, DefinedValues.width/3-5));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}