在测量ListView高度时,LinearLayout.measure上的ClassCastException

时间:2016-11-07 09:30:36

标签: android android-layout listview

在我的Android应用程序中,有一个包含两个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>

如您所见,ListView的项目中有两个视图。一个是TextView,另一个是ImageView。其中一个将一次显示,仅显示TextView或仅显示ImageView。 当我只显示TextView时没有错误,但是当我只显示ImageView时(在TextView上通过set visible =“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

有关更多详细信息,我使用自定义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;
}

如何解决此错误? 提前谢谢!!

1 个答案:

答案 0 :(得分:0)

请注意,看看你的logcat。你骂了

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

而不是

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