2017-02-28 1 views
0

자동차 이미지와 모델 이름으로 GridView를 채워야하는 어댑터를 만들고 있습니다. 원하는 위젯 (ImageView 및 TextView)이있는 자체 XML 파일로 그리드 항목을 만들었습니다. 그러나 내 CarsGridViewItem 인스턴스 내에서보기를 팽창시킬 수 없습니다. 그러나 내 어댑터의 getView -method에서보기를 팽창 시키면 작동합니다.인스턴스 내부에서 팽창하는보기가 작동하지 않음

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

뷰를 배치하고 디버깅하여 볼 수 있습니다. '레이아웃 경계 표시 '또는'도구 -> 안드로이드 -> 레이아웃 검사기'를 선택하십시오. 너의 시야가 0 인 것 같아. – azizbekian

+0

레이아웃 검사기와 함께 멋진 팁. 이것은'CarsGridViewItem'은 너비와 높이를 가지고있는 반면, 확장 된 RelativeLayout은 너비와 높이가 없음을 보여줍니다! 무엇이 이것을 일으킬 수 있습니까? –

+0

또한'CarsGridViewItem' 클래스를 게시하십시오. – azizbekian

답변

1

CarsGridViewItem에서 onMeasure()onLayout() 메서드를 제거하거나 올바르게 구현하십시오. 올바르게 다시 정의하지 않았기 때문에 올바르게 구현하십시오.

onLayout()을 오버라이드했으며 아무 것도 수행하지 않으므로 아무 것도 배치되지 않습니다. 수퍼 클래스가 당신을 위해보기를 레이아웃하게하십시오.

+0

onLayout 메서드를 재정의 한 이유가 무엇인지 모릅니다. 내 IDE가 어떤 이유로 추가했다고 생각합니다 ... 대단히 감사합니다. 대단히 감사합니다! –

관련 문제