如何在scrollview中放置可扩展的列表视图和线性布局

时间:2016-06-16 06:06:46

标签: android

这是我用来显示的代码

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/llheadings">

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


        <ExpandableListView
            android:id="@+id/myExpanded"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/llheadings"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="30dp"
            android:layout_marginLeft="50dp"
            android:childDivider="@android:color/transparent" />

        <LinearLayout
            android:id="@+id/layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/myExpanded"
            android:layout_marginLeft="50dp"
            android:orientation="vertical">

        </LinearLayout>
    </LinearLayout>
</ScrollView>

但是会发生什么是ExpandableListView高度仅包装项目。and here is the snapshot

如果我设置hieght(例如:android:layout_height =“100dp”),则exapandapble listview变为不可滚动

1 个答案:

答案 0 :(得分:0)

只需进行ExpandableListInScrollHelper.java课程即可进行正确的滚动并添加以下方法。

public static void getExpandableListViewSize(ExpandableListView myListView) {
    ListAdapter myListAdapter = myListView.getAdapter();
    if (myListAdapter == null) {
        //do nothing return null
        return;
    }
    //set listAdapter in loop for getting final size
    int totalHeight = 0;
    for (int size = 0; size < myListAdapter.getCount(); size++) {
        View listItem = myListAdapter.getView(size, null, myListView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }
    //setting listview item in adapter
    ViewGroup.LayoutParams params = myListView.getLayoutParams();
    params.height = totalHeight + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
    myListView.setLayoutParams(params);
    // print height of adapter on log
    Log.i("height of listItem:", String.valueOf(totalHeight));
}

现在,在设置适配器下添加一行代码,如

mExpandableList.setAdapter(mAdapter);
ExpandableListInScrollHelper.getExpandableListViewSize(mExpandableList);

现在你的问题已经解决了!