我应该想要一个不可滚动的ListView,并显示整个ListView.这是因为我的整个屏幕都是ScrollView,我用RelativeLayout调度小部件,所以我不需要ListView滚动.
我用代码设置我的ui,而不是用xml.
我用过listView.setScrollContainer(false),但它不起作用,我不明白为什么.
谢谢.
ElY*_*nte 14
正确的答案在这里.
只需将此侦听器分配给您ListView:
listView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
return true; // Indicates that this has been handled by you and will not be forwarded further.
}
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
Chr*_*623 14
我找到了一个非常简单的解决方案.只需获取列表视图的适配器并在显示所有项目时计算其大小.优点是此解决方案也适用于ScrollView.
例:
public 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)
上面显示的功能是对帖子的修改: 禁用列表视图中的滚动
请注意在将适配器设置为列表视图后调用此函数.如果适配器中条目的大小已更改,则还需要调用此函数.
Fal*_*rri 12
不要将列表视图放在滚动视图中,它不起作用.如果您想要一个不滚动的项目列表,则称为线性布局.
| 归档时间: |
|
| 查看次数: |
30112 次 |
| 最近记录: |