2016-11-07 35 views
0

在我的Android應用程序中,有一個包含兩個ListView的佈局。佈局正如你所看到的,也有在ListView的項目兩個視圖實現以下LinearLayout.measure上的ClassCastException當度量ListView的高度

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:dslv="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:id="@+id/scroll_child" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin"> 

     <TextView 
      android:id="@+id/qranking_question" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="horizontal"> 

      <com.mobeta.android.dslv.DragSortListView 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       dslv:collapsed_height="2dp" 
       dslv:drag_enabled="true" 
       dslv:drag_scroll_start="0.33" 
       dslv:drag_start_mode="onLongPress" 
       dslv:float_alpha="0.5" 
       dslv:max_drag_scroll_speed="0.5" 
       dslv:track_drag_sort="false" /> 

      <View 
       android:layout_width="@dimen/seperator_view_height" 
       android:layout_height="match_parent" /> 

      <ListView 
       android:id="@+id/qranking_choice" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" /> 

     </LinearLayout> 
    </LinearLayout> 

</ScrollView> 

而且每個項目在ListView

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/qranking_item_name" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <ImageView 
     android:id="@+id/qranking_item_img" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:scaleType="centerCrop" /> 

</LinearLayout> 

。一個是TextView,另一個是ImageView。其中一個將一次顯示,無論是TextView only還是僅ImageView。 沒有錯誤,當我只顯示TextView的,但是當我在線路

view.measure(...); 
以下方法的

時僅顯示ImageView的(通過設置可見=上TextView的「去」)收到錯誤

private float getListViewHeightBasedOnChildren(ListView listView) { 
    ListAdapter listAdapter = listView.getAdapter(); 
    if (listAdapter == null) 
     return 0.0f; 

    float screenWidth = DeviceProperties.getInstance().getScreenWidthDP(context); // DeviceProperties.getInstance().getScreenWidth(); 
    float seperatorWidth = context.getResources().getDimension(R.dimen.seperator_view_height); 
    float viewHorizontalMargin = context.getResources().getDimension(R.dimen.activity_horizontal_margin); 
    final int FAKE_MARGIN = 2; 
    int desiredWidth = (int) ((screenWidth - seperatorWidth - viewHorizontalMargin)/2) - (2 * FAKE_MARGIN); 
    int totalHeight = 0; 
    View view = null; 

     for (int i = 0; i < listAdapter.getCount(); i++) { 
      QRankingItem item = (QRankingItem) listAdapter.getItem(i); 
      String text = item.getDescription(); 

      view = listAdapter.getView(i, view, listView); 

      if (i == 0) 
       view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, 
         ViewGroup.LayoutParams.MATCH_PARENT)); 

      view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); 
      float measuredHeight = view.getMeasuredHeight(); 
      ... 
     } 

    return totalHeight; 
} 

錯誤說

java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams 

有關詳細信息,我使用自定義的ListView的適配器它返回一個視圖以下

@Override 
public View getView(int i, View view, ViewGroup viewGroup) { 
    view = null; 

    if (view == null) { 
     view = ((LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.listitem_qranking, null); 

     QRankingItem item = mModelList.get(i); 
     TextView descView = (TextView) view.findViewById(R.id.qranking_item_name); 
     ImageView imgView = (ImageView) view.findViewById(R.id.qranking_item_img); 
     float screenWidth = DeviceProperties.getInstance().getScreenWidth(); 
     float imgWidth = (screenWidth/2) - (0.2f * screenWidth); 
     imgView.setLayoutParams(new ViewGroup.LayoutParams((int) imgWidth, (int) imgWidth)); 

     if (item.getType() == TYPE_RANKING.IMAGE) { 
      descView.setVisibility(View.GONE); 
      imgView.setImageBitmap(item.getChoiceImg()); 
     } else { 
      descView.setText(item.getDescription()); 
      imgView.setVisibility(View.GONE); 
     } 
    } 

    return view; 
} 

如何解決此錯誤? 提前謝謝!

回答

0

請看看你的logcat。您sholud使用

imgView.setLayoutParams(new LinearLayout.LayoutParams((int) imgWidth, (int) imgWidth)); 

,而不是

imgView.setLayoutParams(new ViewGroup.LayoutParams((int) imgWidth, (int) imgWidth)) 
+0

這是工作!感謝您找出錯誤原因(我嘗試將ViewGroup.LayoutParams改爲LinearLayout.LayoutParams,並且仍然導致錯誤)。 –

+0

現在我得到了與DragSortListView相同的錯誤,但首先讓我試着自己弄清楚。 @Sergey –