2016-11-07 1 views
0

私のAndroidアプリケーションでは、2つのListViewを含むレイアウトがあります。レイアウトは、あなたが見ることができるように、リストビューの項目に2つのビューがあるのListViewLinearLayout.measure(メジャー時)ClassCastException 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> 

で、次の

<?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> 

として、各項目のために実装されています。 1つはTextViewで、もう1つはImageViewです。そのうちの1つが、TextViewのみまたはImageViewのみで一度に表示されます。 あり、私は唯一のTextViewを表示するエラーがありませんが、私はImageViewのを(のTextViewにセット目に見える=「GONE」による)を示したときに、以下の方法

の際にラインで

view.measure(...); 

私はエラーを得ました

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 

を言いました

@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

Plese、あなたのlogcatを見てください。あなたはそれが仕事だ代わりに

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

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

を使用しsholud!エラーの原因がどこにあるのか把握していただきありがとうございます(ViewGroup.LayoutParamsの多くをLinearLayout.LayoutParamsに変更しようとしましたが、まだエラーが発生しています)。 –

+0

今、私はDragSortListViewで同じエラーが発生しましたが、まず自分で把握しようとしましょう。 @Sergey –

関連する問題