2013-12-20 122 views
1

我試圖使GridView的列大小更改基於重寫「layoutChildren()」。 但是當我打電話服裝類在我的適配器類拋出異常 這裏是從「Getview」調用代碼:調用擴展gridview類拋出異常

public View getView (int position, View convertView, ViewGroup parent) 
{ 
    mParentView = parent; 

    ImageCell v = null; 
    if (convertView == null) { 
     v = new ImageCell (mContext); 
     v.setLayoutParams(new ResizeableGridView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     v.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     v.setPadding(40, 40, 40, 40); 

    } else { 
     v = (ImageCell) convertView; 
    } 

    v.mCellNumber = position; 
    v.mGrid = (ResizeableGridView) mParentView; 
    v.mEmpty = true; 
    v.setBackgroundResource (R.color.cell_empty); 
started. 
    v.setOnTouchListener ((View.OnTouchListener) mContext); 
    v.setOnClickListener ((View.OnClickListener) mContext); 
    v.setOnLongClickListener ((View.OnLongClickListener) mContext); 

    return v; 
} 

和「ResizeableGridView」:從logcat的

package com.package.mypackage; 

import android.content.Context; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.GridView; 

public class ResizeableGridView extends GridView { 

private int height; 

public ResizeableGridView(Context context) { 
    super(context); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    height = getHeight(); 
} 

@Override 
protected void layoutChildren() { 
    super.layoutChildren(); 
    View v = getChildAt(0); 
    if((v.getHeight()*5) < height){ 
     //if the combined height of rows is less than parent then only resize the children to fit parent 
     int count = getChildCount(); 
     for(int i = 0;i < count;i++){ 
      View view = getChildAt(i); 
      ViewGroup.LayoutParams params = view.getLayoutParams(); 
      //I am using 5 because i used it for calendar and it has exactly five rows 
      params.height = height/5; 
      view.setLayoutParams(params); 
     }//end for 
    }//end if 
} // end layoutChildren 
} 

和錯誤日誌:

12-20 10:40:41.382: E/AndroidRuntime(2646): FATAL EXCEPTION: main 
12-20 10:40:41.382: E/AndroidRuntime(2646): java.lang.ClassCastException: android.widget.GridView 
12-20 10:40:41.382: E/AndroidRuntime(2646):  at com.package.mypackage.ImageCellAdapter.getView(ImageCellAdapter.java:81) 
12-20 10:40:41.382: E/AndroidRuntime(2646):  at android.widget.AbsListView.obtainView(AbsListView.java:1592) 
12-20 10:40:41.382: E/AndroidRuntime(2646):  at android.widget.GridView.onMeasure(GridView.java:955) 
12-20 10:40:41.382: E/AndroidRuntime(2646):  at android.view.View.measure(View.java:8313) 
12-20 10:40:41.382: E/AndroidRuntime(2646):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) 
12-20 10:40:41.382: E/AndroidRuntime(2646):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017) 
12-20 10:40:41.382: E/AndroidRuntime(2646):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:386) 
12-20 10:40:41.382: E/AndroidRuntime(2646):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:309) 
12-20 10:40:41.382: E/AndroidRuntime(2646):  at android.view.View.measure(View.java:8313) 
12-20 10:40:41.382: E/AndroidRuntime(2646):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) 
12-20 10:40:41.382: E/AndroidRuntime(2646):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017) 
12-20 10:40:41.382: E/AndroidRuntime(2646):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:386) 
12-20 10:40:41.382: E/AndroidRuntime(2646):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:309) 
12-20 10:40:41.382: E/AndroidRuntime(2646):  at android.view.View.measure(View.java:8313) 
12-20 10:40:41.382: E/AndroidRuntime(2646):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) 

回答

0

ViewGroup.LayoutParams params = view.getLayoutParams();這是造成異常,您必須使用

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(your_height,your_width); 
convertView.setLayoutParams(new GridView.LayoutParams(params)); 

如果容器是LinearLayout