【问题标题】:How to set the ListView height如何设置 ListView 的高度
【发布时间】:2013-03-10 13:59:46
【问题描述】:

我想知道如何通过计算item view来设置ListView的高度,有人这样说。

ListAdapter listAdapter = listView.getAdapter();

if (listAdapter == null) {
    return;
}

int totalHeight = 0;

for (int i = 0; i < listAdapter.getCount(); i++) {
    View listItem = listAdapter.getView(i, null, listView);
    listItem.measure(0, 0);
    totalHeight += listItem.getMeasuredHeight();
}

但是,如果我为基于BaseAdapter 的课程添加ListView 会怎样?

【问题讨论】:

  • 你想把它放在列表视图中的数据是静态的吗?还是动态的?
  • 动态!那我将使用“adapter.notifyDataSetChanged();”来更新适配器
  • 所以在dynamic的情况下,您是否在适配器内部使用特定结构来保存数据,例如hashmaparraylist,或者您让您的适配器从外部源读取数据比如server connection或者database?

标签: android listview listadapter baseadapter


【解决方案1】:

BaseAdapter 仅实现 ListAdapter 适配器接口,因此您的代码没有理由不能与 BaseAdapter 一起使用。你可以试试这个:

BaseAdapter listAdapter = (BaseAdapter)listView.getAdapter();

if (listAdapter == null) {
    return;
}

int totalHeight = 0;

for (int i = 0; i < listAdapter.getCount(); i++) {
    View listItem = listAdapter.getView(i, null, listView);
    listItem.measure(0, 0);
    totalHeight += listItem.getMeasuredHeight();
}

【讨论】:

    猜你喜欢
    • 2015-03-16
    • 2012-11-29
    • 1970-01-01
    • 2011-04-04
    • 2014-08-27
    • 1970-01-01
    • 2014-09-11
    • 1970-01-01
    • 2014-12-15
    相关资源
    最近更新 更多