如何使整个屏幕包含(LinearLayout + ListView)可滚动

时间:2021-01-07 19:17:04

I have an Android fragment in which contains a Vertical LinearLayout with two layouts inside it: Another inner Vertical LinearLayout (which takes at least half of the screen) and a ListView (which shows a number of smaller Views). A good way to think about it is something like a facebook post along with several comments showing below it.

我有一个Android片段,其中包含一个Vertical LinearLayout,里面有两个布局:另一个内部Vertical LinearLayout(至少需要屏幕的一半)和一个ListView(显示一些较小的视图)。考虑它的好方法就像facebook帖子以及下面显示的几条评论。

The problem I am facing is that the the inner LinearLayout is not scrollable and therefore parts of the ListView stays outside of the screen and there is no way to scroll the screen up to see the entire ListView (the inner layout could possibly take up the entire screen). Placing the entire layout inside a ScrollView is also known to be a bad practice.

我面临的问题是内部LinearLayout不可滚动,因此ListView的一部分停留在屏幕之外,没有办法向上滚动屏幕以查看整个ListView(内部布局可能占用整个屏幕)。将整个布局放在ScrollView中也是一种不好的做法。

I know one solution is to make the entire layout to be a ListView with the first cell to be the inner LinearLayout, followed by the cells from the original ListView. That however has its own issues: It is harder to recycle the views in the ListView, and generally I also see it as a bad practice where one cell has a different behavior from all other cells in the ListView.

我知道一个解决方案是使整个布局成为ListView,第一个单元格是内部LinearLayout,然后是原始ListView中的单元格。然而,这有其自身的问题:在ListView中回收视图更难,通常我也认为这是一种不好的做法,其中一个单元格与ListView中的所有其他单元格具有不同的行为。

Is there a better solution to this? I can imagine this is a typical problem and I hope to find a better solution than above.

有更好的解决方案吗?我可以想象这是一个典型的问题,我希望找到一个比上面更好的解决方案。

1 个解决方案

#1


This may help you:

这可能对您有所帮助:

ListView yourListView = new ListView(this);
yourListView.addHeader(getHeaderView());

You can also add footer to a ListView. When you do this, the header(or footer) will become part of the ListView, and so it will be scrollable. You can find the document here: http://developer.android.com/reference/android/widget/ListView.html.

您还可以将页脚添加到ListView。执行此操作时,页眉(或页脚)将成为ListView的一部分,因此它将可滚动。您可以在此处找到该文档:http://developer.android.com/reference/android/widget/ListView.html。

#1


This may help you:

这可能对您有所帮助:

ListView yourListView = new ListView(this);
yourListView.addHeader(getHeaderView());

You can also add footer to a ListView. When you do this, the header(or footer) will become part of the ListView, and so it will be scrollable. You can find the document here: http://developer.android.com/reference/android/widget/ListView.html.

您还可以将页脚添加到ListView。执行此操作时,页眉(或页脚)将成为ListView的一部分,因此它将可滚动。您可以在此处找到该文档:http://developer.android.com/reference/android/widget/ListView.html。