私は車の画像とモデル名でGridViewを設定するアダプタを作成しています。私は、望みのウィジェット(ImageViewとTextView)を使って、独自のXMLファイルとしてグリッドアイテムを作成しました。しかし、私はCarsGridViewItem
インスタンス内からビューを膨らませることはできません。しかし、アダプタのgetView
の方法からビューを膨らませると動作します。インスタンス内でインフレしているビューが動作しない
CarsGridViewItem
インスタンス内からビューを膨張させるとどうなりますか、私は膨張されるはずのビューを見ることができません。
以下は私の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) {
}
}
そして、私のアダプター私はここに何か間違ったことをやっている
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;*/
}
のgetView
-methodですが、私は何を把握するように見えることはできません。ビューを膨らませるという話題で私が見つけたすべての例は、このようなものです!
デバッグして、ビューの配置方法を確認できます。 'レイアウトの境界を表示する'または 'ツール - > Android - >レイアウトインスペクタ'をクリックします。あなたの意見は0の高さを持っているようです。 – azizbekian
レイアウトインスペクタの素晴らしいヒント。これは、 'CarsGridViewItem'が幅と高さを持っているのに対し、膨張したRelativeLayoutは幅と高さがないことを示しています!これを引き起こす原因は何ですか? –
'CarsGridViewItem'クラスも投稿してください。 – azizbekian