2013-04-07 63 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