今天來介紹一些ListView的用法
主要目的是當ListView拉到頂時自動重新整理、拉到底時自動載入更多。
一開始先建立兩個layout,分別是head.xml、foot.xml
分別用來顯示重新整理中、載入中的文字
接下來直接來看主要的程式碼
這些變數是用來判斷要不要更新或載入資料
boolean shouldRefresh=true,isRefreshing=false;
boolean shouldLoadData=true,isLoadingData=false;
head = LayoutInflater.from(this).inflate(R.layout.head, null);//將head.xml轉為view
headtext = (TextView) head.findViewById(R.id.head);//取得head裡的TextView
foot = LayoutInflater.from(this).inflate(R.layout.foot, null);//將foot.xml轉為view
list = getListView();//取得ListView
list.addHeaderView(head, null, false);//就addHeaderView...
list.addFooterView(foot, null, false);//就addFooterView...
mydataAdapter = new MydataAdapter(Main.this,alldata);
setListAdapter(mydataAdapter);//一定要放在addHeaderView及addFooterView的後面