我试图在Overriding" layoutChildren()"的基础上改变GridView列大小。 但是当我打电话给服装类时,它会抛出异常 这是来自" Getview"的调用代码。在我的适配器类中:
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" :
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
}
来自logcat的错误日志:
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 :(得分:0)
ViewGroup.LayoutParams params = view.getLayoutParams();
这导致异常,您必须使用
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(your_height,your_width);
convertView.setLayoutParams(new GridView.LayoutParams(params));
如果容器是LinearLayout