2017-02-28 4 views
0

私は車の画像とモデル名で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ですが、私は何を把握するように見えることはできません。ビューを膨らませるという話題で私が見つけたすべての例は、このようなものです!

+0

デバッグして、ビューの配置方法を確認できます。 'レイアウトの境界を表示する'または 'ツール - > Android - >レイアウトインスペクタ'をクリックします。あなたの意見は0の高さを持っているようです。 – azizbekian

+0

レイアウトインスペクタの素晴らしいヒント。これは、 'CarsGridViewItem'が幅と高さを持っているのに対し、膨張したRelativeLayoutは幅と高さがないことを示しています!これを引き起こす原因は何ですか? –

+0

'CarsGridViewItem'クラスも投稿してください。 – azizbekian

答えて

1

CarsGridViewItemからonMeasure()onLayout()のメソッドを削除するか、正しく実装していないため正しく実装してください。

あなたはonLayout()を上書きしていますが、何もしていないので、レイアウトされているものはありません。あなたのためにスーパークラスレイアウトを表示させてください。

+0

なぜ私はonLayoutメソッドをオーバーライドしたのか分かりません。私のIDEが何らかの理由でそれを追加したと思います。 –

関連する問題