Reputation: 3707
I'm creating an adapter that should populate a GridView with images of cars, and a model name. I have created a grid item as an own XML-file, with the desired widgets (ImageView and TextView). However, I can't seem to inflate the view from within my CarsGridViewItem
instance. It works if I inflate the view from the getView
-method of my adapter however.
What happens if I inflate the view from within the CarsGridViewItem
instance, is that I can't see the view that's supposed to be inflated.
Below is my CarsGridViewItem
class
public class CarsGridViewItem extends RelativeLayout {
private Car car;
private ImageView carImg;
private TextView nameTxt;
public CarsGridViewItem(Car car, Context context) {
super(context);
this.car = car;
inflate(getContext(), R.layout.fragment_cars_grid_item, this);
findViews();
setupViews();
}
private void findViews() {
this.carImg = (ImageView)findViewById(R.id.car_image);
this.nameTxt = (TextView)findViewById(R.id.car_name);
}
private void setupViews(){
this.car.loadImageIntoView(this.carImg);
this.nameTxt.setText(this.car.getName());
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
@Override
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
}
}
And the getView
-method of my adapter
public View getView(int position, View convertView, ViewGroup parent){
return new CarsGridViewItem(cars.get(position), mContext);
/* The code below works!
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.fragment_cars_grid_item, null);
ImageView carImg = (ImageView)view.findViewById(R.id.car_image);
cars.get(position).loadImageIntoView(carImg);
TextView nameTxt = (TextView)view.findViewById(R.id.car_name);
nameTxt.setText(cars.get(position).getName());
return view;*/
}
I'm doing something wrong here, but I can't seem to figure out what. All the examples I've found on the topic of inflating views does it like this!
Upvotes: 0
Views: 36
Reputation: 62189
Either remove onMeasure()
and onLayout()
methods from CarsGridViewItem
or implement them correctly, because you do not override them correctly now.
You have overriden onLayout()
and are doing nothing there, thus nothing is being laid out. Let the super class layout the view for you.
Upvotes: 1