Android Listview多次调用相同的addHeaderView和addFooterView 问题

原文地址http://blog.csdn.net/jdsjlzx/article/details/42388467


在添加addheadview或者footview时 先判断当前的listview 是不是已经添加过了,

 
  
getFooterViewsCount
 
  
getHeaderViewsCount
两个方法,

 
  
private void setView(ListView listView, String s, View view) {

    switch (s) {
        case "footview":
            if (listView.getFooterViewsCount() == 0) {
                listView.addFooterView(view);
            }
            break;
        case "headview":
            if (listView.getHeaderViewsCount() == 0) {
                listView.addHeaderView(view);
            }
            break;
    }


}
调用
 
  
View title0 = View.inflate(context, R.layout.plandetails_headtitle, null);
TextView textView = (TextView) title0.findViewById(R.id.listView_details_title);
textView.setText("收到5个橄榄枝");
View footview0 = View.inflate(context, R.layout.footview_view_plan_details, null);

setView(lstMoreRec, "footview", footview0);
setView(lstMoreRec,"headview", title0);
lstMoreRec.setAdapter(adapter_recomment);



你可能感兴趣的:(小经验,android,listview,headView)