从实例中膨胀视图不起作用
Inflating view from within instance not working
我正在创建一个适配器,它应该用汽车图像和模型名称填充 GridView。我创建了一个网格项目作为自己的 XML 文件,其中包含所需的小部件(ImageView 和 TextView)。但是,我似乎无法从我的 CarsGridViewItem
实例中放大视图。但是,如果我从我的适配器的 getView
方法中膨胀视图,它就会工作。
如果我从 CarsGridViewItem
实例中扩充视图,会发生什么情况,即我看不到应该扩充的视图。
下面是我的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) {
}
}
以及我的适配器的getView
-方法
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;*/
}
我在这里做错了什么,但我似乎无法弄清楚是什么。我找到的关于膨胀视图主题的所有示例都是这样的!
要么从 CarsGridViewItem
中删除 onMeasure()
和 onLayout()
方法,要么正确实施它们,因为您现在没有正确覆盖它们。
您已覆盖 onLayout()
并且在那里什么都不做,因此没有布置任何内容。让超级 class 为您布置视图。
我正在创建一个适配器,它应该用汽车图像和模型名称填充 GridView。我创建了一个网格项目作为自己的 XML 文件,其中包含所需的小部件(ImageView 和 TextView)。但是,我似乎无法从我的 CarsGridViewItem
实例中放大视图。但是,如果我从我的适配器的 getView
方法中膨胀视图,它就会工作。
如果我从 CarsGridViewItem
实例中扩充视图,会发生什么情况,即我看不到应该扩充的视图。
下面是我的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) {
}
}
以及我的适配器的getView
-方法
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;*/
}
我在这里做错了什么,但我似乎无法弄清楚是什么。我找到的关于膨胀视图主题的所有示例都是这样的!
要么从 CarsGridViewItem
中删除 onMeasure()
和 onLayout()
方法,要么正确实施它们,因为您现在没有正确覆盖它们。
您已覆盖 onLayout()
并且在那里什么都不做,因此没有布置任何内容。让超级 class 为您布置视图。