gpt4 book ai didi

java - 从实例内部 inflatedView 不起作用

转载 作者:行者123 更新时间:2023-12-01 08:48:52 25 4
gpt4 key购买 nike

我正在创建一个适配器,该适配器应使用汽车图像和型号名称填充 GridView。我已经创建了一个网格项作为自己的 XML 文件,其中包含所需的小部件(ImageView 和 TextView)。但是,我似乎无法从我的 CarsGridViewItem 实例中 inflatedView 。但是,如果我通过适配器的 getView 方法来 inflatedView ,它就会起作用。

如果我从 CarsGridViewItem 实例中 inflatedView ,会发生什么情况,我看不到应该膨胀的 View 。

下面是我的 CarsGridViewItem

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;*/
}

我在这里做错了什么,但我似乎不知道是什么。我发现的有关 View 膨胀主题的所有示例都是这样的!

最佳答案

CarsGridViewItem 中删除 onMeasure()onLayout() 方法或正确实现它们,因为您现在没有正确覆盖它们。

您已经重写了 onLayout() 并且没有执行任何操作,因此没有任何内容被布局。让父类(super class)为您布局 View 。

关于java - 从实例内部 inflatedView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42513154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com