2013-04-07 62 views
0

根據屏幕大小,我想動態設定的RelativeLayout的高度。另一種方法是使用setMinimumHeight(),但我仍然想知道爲什麼這會崩潰。RelativeLayout.setLayoutParams導致viewholder模式崩潰

public View getView(final int position, View convertView, ViewGroup parent) { 
     ViewHolder vh; 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.question_row_inflater, null); 
      vh = new ViewHolder(); // will store references to views 
      vh.answerBlock = (RelativeLayout) convertView.findViewById(R.id.answerBlock); 
      convertView.setTag(vh); // store vh away 
     } else { 
      vh = (ViewHolder) convertView.getTag(); 
     } 

     int height = mRes.getDisplayMetrics().heightPixels; 
     // workaround for small screens 
     if (height < 490) 
      RelativeLayout.LayoutParams par = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, 30); 
      vh.answerBlock.setLayoutParams(par); 
     } 
     return convertView; 
    } 

這是classcast例外:enter image description here

<RelativeLayout 
     android:id="@+id/answerBlock" 
     android:layout_width="fill_parent" 
     android:layout_height="@dimen/list_height" 
     android:layout_weight="1" 
     android:clickable="true" 
     android:paddingLeft="16dp" > 

     <TextView 
      android:id="@+id/answerText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
    </RelativeLayout> 

回答

0

你所得到的類轉換異常,因爲你在呼喚vh.answerBlock.setLayoutParams(params);這將在ViewGroup(見docs)的參數。

因此,我可以推測,answerBlockRelativeLayoutLinearLayout的孩子(也堆棧跟蹤是告密者)。

的溶液(該異常至少)是使用匹配視圖的父該LayoutParams