How do I use ListView as one of the children of a ScrollView

怎甘沉沦 提交于 2020-01-05 12:11:40

问题


I have read a number of literatures that say not to do this. But they have yet to offer an alternative solution. The question seems to come up a lot, though. Here is my problem.

  • I want my main layout to be scrollable (this is because it will have more views than can fit the screen). My main layout is a vertical LinearLayout. So for now I have it inside a ScrollView.

  • one of the many children of my LinearLayout is a ListView (shows 3 items, user scrolls the ListView for more).

  • You can image the other children of my LinearLayout to be whatever will convince you that I need this to work: for example, one may be a scrollable horizontal LinearLayout; another may be a ViewPater; etc.

Now if the content of my LinearLayout could all fit the screen at once, I would have no problem. But I have about six different children in my LinearLayout and only about three are visible at one time. So, yes, I do need to be able to scroll my LinearLayout.

One more thing. If I am scrolling the child ListView and I reach the end, I need the parent LinearLayout to resume control and start scrolling as user scrolls.

CLARIFICATION

One clarification is that my ListView is actually dynamically added to a ViewPager. So technically, a ViewPager is the child of my LinearLayout. But then inside the ViewPager, one of the pages is a ListView.

NOTE

By the way, I have read scrolling ListView within ScrollView. The answer there is good, but I still have a few problems.

  • once edge of ListView is reached control does not revert to ScrollView

  • If any of the other child views of the LinearLayout have a click listener, then I cannot use them to scroll the ScrollView. So basically for most of the screen, the scroll view is not scrollable.


回答1:


You could use the ListView as main layout (instead of the ScrollView) and put the views on top with the lists addHeaderView()-method (grouped in a LinearLayout or something like that) and do the same with the items below the list, with the addFooterView-method of the ListView.

I did not test it, but I think it might work.



来源:https://stackoverflow.com/questions/20289065/how-do-i-use-listview-as-one-of-the-children-of-a-scrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!