滚动视图中的listview不适用于低于API 18的android版本



ScrollView内部的ListView在大于18的android API上运行良好,但低于API 18的应用程序崩溃显示java.lang.NullPointerExceptionlistItem.measure(0,0)在MyUtils.java 上

MyUtils.java

    public static void setListViewHeightBasedOnChildren(ExpandableListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }

fragment_list.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="sportsnewsforcrazysportpepllatestnewsjstwaao.com.sportsnews.fragments.Frag_List">
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <com.android.volley.toolbox.NetworkImageView
            android:id="@+id/networkImageView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:background="@color/divider"
            android:minHeight="150dp"/>
        <ExpandableListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/expandableListView"
            android:scrollbars="none"
            android:background="@color/app_background"
            android:childDivider="@color/app_background"/>
    </LinearLayout>
</ScrollView>

Fragment_list.java

    adapter = new ExpandableListViewAdapter(getActivity() , dataList);
    expandableListView.setAdapter(adapter);
    MyUtils.setListViewHeightBasedOnChildren(expandableListView);
    expandableListView.setOnGroupClickListener(this);
    expandableListView.setOnChildClickListener(this);

在我的应用程序中,如果listview项布局的根视图是RelativeLayout,则会导致此崩溃。因为在api 18 RelativeLayout源代码

if(mLayoutParams.width >= 0) { width = Math.max(width, mLayoutParams.width); }

并且在api中>=19 RelativeLayout源代码

if(mLayoutParams != null && mLayoutParams.width >= 0) { width = Math.max(width, mLayoutParams.width); }

如果你用这种方法膨胀项目视图,那么mLayoutParams可能为空:

contentView = (ViewGroup) LayoutInflater. rom(context).inflate(R.layout.select_text_oprate_window, null);

所以,你可以使用这种方法:

contentView = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.select_text_oprate_window, parent,false);

您还可以将项目布局的根视图更改为LinearLayout。

最新更新