嗯,我正在尝试在更改数据后获取我的listview高度,但它总是返回先前的高度而不是实际高度。
所以当我设置setadapter时,它会获取旧值。 例如:
ActualHeight = 100
Change data (filter) -> NewHeight = 60
ListView.GetHeight still returns 100.
Again
ActualHeight = 60
Change data (filter) -> NewHeight = 20
ListView.GetHeight still returns 60.
我使用的代码如下:
这个代码是这样的:
int width, height = 0;
EditText edt_search;
ListView lv_marca;
List<Marca> list_marca = new ArrayList<Marca>();
List<Marca> list_marca_search = new ArrayList<Marca>();
String text = edt_search.getText().toString();
list_marca_search.clear();
if(text.startsWith(".")){
text = text.replace(".", "");
for (Marca m : list_marca) {
if (String.valueOf(m.getCd_marca()).equals(text)){
list_marca_search.add(m);
}
}
} else {
for (Marca m : lista_marca) {
if (m.getDs_marca().contains(text)){
list_marca_search.add(m);
}
}
}
ArrayAdapter<Marca> adapter_marca = new CustomAdapter_Marca(MyDialog.this, R.layout.layout_consulta_estoque_marca_lista, list_marca_search);
lv_marca.setAdapter(adapter_marca);
int height_window = getWindowManager().getDefaultDisplay().getHeight();
height = lv_marca.getHeight() + getSupportActionBar().getHeight();
if (height >= height_window) {
height = (int) (height_window * 0.95);
}
getWindow().setLayout(width, height);
ListItem
高度总和,而不是ListView
高度n次。请参见http://stackoverflow.com/a/11967081/8524以获取更好的解决方案。 - Diederik