Cha*_*ake 35 android listview scrollview
我想为我的用户显示个人资料屏幕.
它必须有三个视图(2 Buttons和a ImageView)和a ListView以显示该用户所做的内容.
但是,我不想ListView滚动.相反,我希望它与所需的一样大,并将我的所有视图放在一个中ScrollView,所以三个第一个视图滚动出来ListView.当然,这不符合预期.
我的三件物品都在里面LinearLayout.我想把它们作为第一项ListView,但这导致它们可以作为第一项选择,并且不得不做一些不需要的编码.
有没有办法以简单的方式做到这一点,还是我必须坚持将布局作为ListView中的第一项?
Chr*_*623 48
我找到了一个非常简单的解决方案.只需获取列表视图的适配器并在显示所有项目时计算其大小.优点是此解决方案也适用于ScrollView.
例:
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();
}
Run Code Online (Sandbox Code Playgroud)
调用此函数传递ListView对象:
justifyListViewHeightBasedOnChildren(myListview);
Run Code Online (Sandbox Code Playgroud)
上面显示的功能是对帖子的修改: 禁用列表视图中的滚动
请注意在将适配器设置为列表视图后调用此函数.如果适配器中条目的大小已更改,则还需要调用此函数.
Sai*_*ker 11
你可以这样做
listView.setScrollContainer(false);
Run Code Online (Sandbox Code Playgroud)
更多请检查
我会添加一个View隐形背景ListView.设置View.OnTouchListener()为它.并通过返回消耗事件true的onTouch()方法View.OnTouchListener().
如果希望列表再次向后滚动,请删除透明表上的触摸侦听器集 View
| 归档时间: |
|
| 查看次数: |
83934 次 |
| 最近记录: |