ScrollView distrubing ListView inside of LinearLayout

后端 未结 5 2018
长发绾君心
长发绾君心 2021-01-19 07:00

I have LinearLayout whcih contains Buttons and ListViews. After getting many buttons and I decide to put that LinearLayout into ScrollView but after putting ScrollView my Li

5条回答
  •  后悔当初
    2021-01-19 07:09

    There is a conflict between the ScrollView and the ListView. In general, you cannot put a ListView inside a ScrollView. ListView already understands about scrolling and will scroll itself. ListView needs to know how much vertical space on the screen it should use, so that it can control the scrolling and display of its children. It is difficult to understand how the layout that you've posted should behave.

    In any case, the only way to put a ListView inside a ScrollView is to tell the ListView exactly how much vertical space it should use. You cannot use match_parent or wrap_content for layout_height, you need to fix the vertical size. This helps the ScrollView to determine how much vertical space each of its children takes up, and then the ScrollView can do the right thing.

    Try setting android:layout_height="80dp" on all of your ListViews to see how this behaves.

    EDIT: After looking at the layout again, I have another suggestion

    After looking at the layout again, I am assuming that you want to have only one of the ListViews visible at a given time (the others would all be GONE). In this case, you might try the following:

    1. Remove the surrounding ScrollView
    2. Set android:layout_weight="1" and android:layout_height="1px" on all ListViews

    This basically tells the layout manager that the ListView should be given all available space on the screen (ie: whatever is left over after the other Views are laid out). This will make the ListView expand to take up all available space on the screen, while keeping all the other Views on screen and the entire screen will not scroll, only the ListView. This may be a better way to give you what you want.

    You might also consider using ExpandableListView, which seems to be what you are trying to recreate here.

提交回复
热议问题