Android 禁用包含在 ScrollView 中的 ListView 的滚动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12212890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable scrolling of a ListView contained within a ScrollView
提问by Charlie-Blake
I want to show a Profile screen for my users.
我想为我的用户显示个人资料屏幕。
It must have three views (2 Buttons
and a ImageView
) and a ListView
to show the content made by that user.
它必须具有三个视图(2Buttons
和 a ImageView
)和 aListView
以显示该用户制作的内容。
However, I don't want the ListView
to scroll. Instead, I want it to be as big as needed, and to put all my views inside a ScrollView
, so the three first views scroll out with the ListView
. This, of course, does not work as intended.
但是,我不想ListView
滚动。相反,我希望它尽可能大,并将我的所有视图放在 a 中ScrollView
,因此前三个视图与ListView
. 当然,这不会按预期工作。
All my three items are inside a LinearLayout
. I thought of making them the first item in the ListView
, but this leads to them being selectable as the first item, and having to do some unneeded coding.
我所有的三个项目都在一个LinearLayout
. 我想将它们ListView
作为 .
Is there a way to do this the easy way or will I have to stick with making the Layout the first item in my ListView?
有没有一种简单的方法可以做到这一点,还是我必须坚持将布局作为我的 ListView 中的第一项?
采纳答案by Yalla T.
Adding them to the ListView as first Item seems like a pretty good solution.
将它们作为第一个 Item 添加到 ListView 似乎是一个很好的解决方案。
To make the View unselectable just get the view and .setClickable(false)
.
要使视图不可选择,只需获取视图和.setClickable(false)
.
回答by Chris623
I found a very simple solution for this. Just get the adapter of the listview and calculate its size when all items are shown. The advantage is that this solution also works inside a ScrollView.
我为此找到了一个非常简单的解决方案。只需获取列表视图的适配器并在显示所有项目时计算其大小。优点是该解决方案也适用于 ScrollView。
Example:
例子:
public static void justifyListViewHeightBasedOnChildren (ListView listView) {
ListAdapter adapter = listView.getAdapter();
if (adapter == null) {
return;
}
ViewGroup vg = listView;
int totalHeight = 0;
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, vg);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams par = listView.getLayoutParams();
par.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
listView.setLayoutParams(par);
listView.requestLayout();
}
Call this function passing over your ListView object:
调用此函数传递您的 ListView 对象:
justifyListViewHeightBasedOnChildren(myListview);
The function shown above is a modidication of a post in: Disable scrolling in listview
上面显示的功能是一个帖子的修改: 禁用列表视图中的滚动
Please note to call this function after you have set the adapter to the listview. If the size of entries in the adapter has changed, you need to call this function as well.
请注意在将适配器设置为列表视图后调用此函数。如果适配器中条目的大小发生了变化,您也需要调用此函数。
回答by Saifuddin Sarker
You can do this by
你可以这样做
listView.setScrollContainer(false);
for more please check
更多请查看
回答by Sudarshan Bhat
I would add a View
with invisible background on top of ListView
. Set a View.OnTouchListener()
to it. And consume the event by returning true
in onTouch()
method of View.OnTouchListener()
.
我会View
在ListView
. 设置一个View.OnTouchListener()
。并通过true
在 的onTouch()
方法中返回来消费事件View.OnTouchListener()
。
When you want the list to be scrolling back again, remove the touch listener set on the transparent View
当您希望列表再次向后滚动时,请删除透明设置上的触摸侦听器 View
回答by Usman Nisar
if you have to show limited number of items in list view and want to stop list view from scrolling then you must keep listview height greater then the items total height.
如果您必须在列表视图中显示有限数量的项目并希望停止滚动列表视图,那么您必须保持列表视图高度大于项目总高度。
for example you want to show 3 items. (height of row is 30). then items total height becomes 3 x 30dp = 90dp,
例如,您要显示 3 个项目。(行高为 30)。然后项目总高度变为 3 x 30dp = 90dp,
so now you have to set listview height greater then 90. e.g: 100dp. so now your listview will not scroll in any case.
所以现在您必须将列表视图高度设置为大于 90。例如:100dp。所以现在你的列表视图在任何情况下都不会滚动。
回答by Olivier Bonal
I think the best way would be to put the 2 buttons and the image view in a LinearLayout (or any layout that suits your need) and add this layout as the list header using the addHeaderView method:
我认为最好的方法是将 2 个按钮和图像视图放在 LinearLayout(或任何适合您需要的布局)中,并使用 addHeaderView 方法将此布局添加为列表标题:
http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View)
http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View)
回答by fredy
with the following instruction:
使用以下指令:
name_lista.getLayoutParams (). height = new_size
new_size is a variable that you will calculate according to the number of elements of your list, for example:
new_size 是一个变量,您将根据列表中的元素数量进行计算,例如:
new_size = 100 * list_size;