原因:主要是由于Listview的寬高不確定, 無法確定取多少View來填充ListView,也就是無法確定運行多少次getView()方法。所以設(shè)計布局的時候盡量把listview固定長寬來提高性能。遇到復雜的布局時,可以選擇動態(tài)測量listview 的高度剃袍。
listView_explorer_content.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
LayoutParams lp;
lp = listView_explorer_content.getLayoutParams();
lp.width = listView_explorer_content.getWidth();
lp.height = listView_explorer_content.getHeight();
if (listView_explorer_content.getHeight() > 0) {
listView_explorer_content.setLayoutParams(lp);
}
listView_explorer_content.getViewTreeObserver().removeOnGlobalLayoutListener(this);//移除OnGlobalLayoutListener捎谨,不然會重復調(diào)用民效。
}
}
});