使用expandableheadergridview在Android L上正常工作但在设备< = 4.4上崩溃

时间:2015-02-27 13:46:52

标签: android android-listview

我在我的应用中使用expandableheadergridview在scrollview中添加gridview。它在Android L上完全正常,但应用程序在设备< = 4.4.4上崩溃。不知道为什么会发生这种情况。 我的布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.hj.app"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<com.hj.app.widgets.CustomScrollView
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <include layout="@layout/feed_view_header_layout" />

        <LinearLayout
            android:id="@+id/hj_photo_desc_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="@dimen/hj_item_height"
            android:background="@android:color/white"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/hj_feed_title_text"
                style="@style/HJAppTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:gravity="center_vertical"
                android:minHeight="@dimen/hj_item_height"
                android:text="This is a Photo"
                android:textColor="@color/hj_grey_color"
                android:textSize="@dimen/hj_font_large"
                android:textStyle="bold" />

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/hj_feed_bg"
                android:clipToPadding="false" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingBottom="@dimen/hj_margin_small"
                android:paddingLeft="@dimen/hj_margin_medium"
                android:paddingRight="@dimen/hj_margin_medium"
                android:paddingTop="@dimen/hj_margin_small" >

                <com.hj.app.widgets.CircularImageView
                    android:id="@+id/user_profile_image"
                    android:layout_width="@dimen/hj_source_pic_size"
                    android:layout_height="@dimen/hj_source_pic_size"
                    android:layout_centerVertical="true"
                    android:src="@drawable/ic_launcher"
                    app:border="true"
                    app:border_color="@android:color/white"
                    app:border_width="@dimen/hj_circular_image_border_width"
                    app:shadow="true" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="@dimen/hj_margin_small"
                    android:layout_marginStart="@dimen/hj_margin_small"
                    android:layout_toEndOf="@+id/user_profile_image"
                    android:layout_toRightOf="@+id/user_profile_image"
                    android:text="Umesh"
                    android:textColor="@color/hj_grey_color"
                    android:textSize="@dimen/hj_font_medium" />
            </RelativeLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/hj_feed_bg"
                android:clipToPadding="false" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:lineSpacingExtra="2dp"
                android:paddingBottom="@dimen/hj_margin_small"
                android:paddingLeft="@dimen/hj_margin_medium"
                android:paddingRight="@dimen/hj_margin_medium"
                android:paddingTop="@dimen/hj_margin_small"
                android:singleLine="false"
                android:text="@string/dummy_desc_text"
                android:textColor="@color/hj_grey_color"
                android:textSize="@dimen/hj_font_small" />

            <TextView
                style="@style/HJAppTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/hj_related_product_text"
                android:textColor="@color/hj_grey_color"
                android:textSize="@dimen/hj_font_medium"
                android:textStyle="bold" />

            <include
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/hj_margin_medium"
                android:layout_marginTop="@dimen/hj_margin_small"
                layout="@layout/hj_related_items_grid_layout" />
        </LinearLayout>
    </LinearLayout>
</com.hj.app.widgets.CustomScrollView>

<include
    android:layout_width="match_parent"
    android:layout_height="@dimen/hj_item_height"
    android:layout_gravity="bottom"
    layout="@layout/hj_bottom_bar_layout" />

<include layout="@layout/hj_toolbar_layout" />

  </FrameLayout>

ExpandableHeaderGridView文件

      package com.hj.app.widgets;

     import android.content.Context;
     import android.util.AttributeSet;
     import android.view.ViewGroup;
     import android.widget.GridView;

 public class ExpandableHeightGridView extends GridView {

boolean expanded = false;

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

public ExpandableHeightGridView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public ExpandableHeightGridView(Context context, AttributeSet attrs,
        int defStyle) {
    super(context, attrs, defStyle);
}

public boolean isExpanded() {
    return expanded;
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    if (isExpanded()) {
        // Calculate entire height by providing a very large height hint.
        // View.MEASURED_SIZE_MASK represents the largest height possible.
        int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    } else {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

public void setExpanded(boolean expanded) {
    this.expanded = expanded;
}

}

它在

行的ExpandableHeightGridView中崩溃
   super.onMeasure(widthMeasureSpec, expandSpec); 

使用isExpandedtrue在onmesure中出现以下错误。

错误:

    java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

无法弄清楚为什么会这样。 请帮助!!!!!

EDIT1: 我的适配器类用于填充网格中的数据。

    package com.hj.app.adapters;

    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.FrameLayout;
    import android.widget.FrameLayout.LayoutParams;
    import android.widget.ImageView;
    import android.widget.TextView;

    import com.hj.app.R;


  public class HJRelatedItemGridAdapter extends BaseAdapter {

   Context mContext;
   int itemHeight;

public HJRelatedItemGridAdapter(Context context) {
    this.mContext = context;
}

@Override
public int getCount() {
    return 10;
}

public void setItemHeight(int itemHeight) {
    this.itemHeight = itemHeight;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = LayoutInflater.from(mContext).inflate(
                R.layout.hj_related_items_grid_item_layout, parent, false);
        GridItemHolder holder = new GridItemHolder(convertView);
        convertView.setTag(holder);
    }
    GridItemHolder holder = (GridItemHolder) convertView.getTag();
    LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
            itemHeight);
    holder.parentLayout.setLayoutParams(lp);
    return convertView;
}

public class GridItemHolder {

    ImageView img;
    TextView titleText;
    FrameLayout parentLayout;

    /**
     * @param itemView
     */
    public GridItemHolder(View view) {

        img = (ImageView) view.findViewById(R.id.hj_grid_image);
        titleText = (TextView) view.findViewById(R.id.hj_grid_title_text);
        parentLayout = (FrameLayout) view.findViewById(R.id.hj_grid_parent);
    }

}
}

1 个答案:

答案 0 :(得分:0)

在适配器类中而不是导入

import android.widget.FrameLayout.LayoutParams;

进口

import android.widget.AbsListView.LayoutParams;

因为网格视图的项目的父项是AbsListView而不是FrameLayout。