简体   繁体   English

如何将可扩展列表视图和线性布局放入滚动视图

[英]How to put expandable listview and linear layout inside scrollview

here is the code am using to display 这是我用来显示的代码

<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>

but what happens is ExpandableListView height is wrappeed only item. 但是发生的是ExpandableListView的高度仅包装了项目。 and here is the snapshot 这是快照

if i set hieght(ex:android:layout_height="100dp") then exapandapble listview becomes unscrollable 如果我设置hieght(ex:android:layout_height =“ 100dp”),则exapandapble listview变得不可滚动

Just make ExpandableListInScrollHelper.java class for making proper scroll and add below method. 只需使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));
}

Now, add one line of code below set adaptor like 现在,在设置适配器下面添加一行代码,例如

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

And now your problem is solved!!! 现在您的问题解决了!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM