原 [Android]LIstView的HeaderView

來源:互聯網
上載者:User

標籤:

目錄[-]
  • (1)添加HeaderView之後尺寸布局被忽略。
  • (2)添加HeaderView之後導致OnItemClickListener的position移位
  • (3)LayoutInflater的infalte()
(1)添加HeaderView之後尺寸布局被忽略。通常添加頭部的方法是 
?
123 LayoutInflater lif = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);View headerView = lif.inflate(R.layout.header, null);mListView.addHeaderView(headerView);

原因: 
lif.inflate(R.layout.header, null)丟失了XML布局中根View的LayoutParam,應該使用的是 
?
1 lif.inflate(R.layout.header, mListView, false);
(2)添加HeaderView之後導致OnItemClickListener的position移位OnItemClickListener介面的方法: 
?
1 void onItemClick(AdapterView<?> parent, View view, int position, long id)

position通常是從0開始的,但是添加了HeaderView之後,position也會將HeaderView的數目計算進去。 
幾個解決辦法: 
1,手動計算真實的position位置: 
?
12345678 final headerCount = 1;mListView.setOnItemClickListener(new OnItemClickListener() {    @Override    public void onItemClick(AdapterView<?> parent, View view,            int position, long id) {        Item item = myAdapter.getItem(position - headerCount);    }});

2,其實上面的步驟ListView已經為我們提供了,所以可以改寫為: 
?
1234567 mListView.setOnItemClickListener(new OnItemClickListener() {    @Override    public void onItemClick(AdapterView<?> parent, View view,            int position, long id) {        Item item = parent.getAdapter().getItem(position);    }});
原因在源碼中有比較清晰的解釋: 
當有headerView被添加時,實際傳遞給ListView的adapter被封裝,parent.getAdapter()返回真實被ListView使用的Adapter(HeaderViewListAdapter),HeaderViewListAdapter的getItem(int)方法處理了position的問題。 

(3)LayoutInflater的infalte()用來呼應第一個問題。LayoutInflater的作用很簡單,就是將XML的布局檔案“翻譯”成相應的View對象,而且出於效能的考慮,LayoutInflater只能處理編譯後的XML檔案,而不能處理通常明文編碼的XML檔案。 
最常用的一個方法: 
?
1 View inflate(int resource, ViewGroup root, boolean attachToRoot)

其中: 
resource是布局檔案ID 
root是父ViewGroup對象, 
attachToRoot是是否將“翻譯”出來的View添加到上面的root中 

root和attachToRoot是共同作用的: 
1,有root,同時attachToRoot為false,那麼inflate()返回的就是“翻譯”得到的view 
2,有root,同時attachToRoot為true,那麼inflate()就是將“翻譯”得到的view添加到root後,然後返回root 
3,無root,同時attachToRoot為false,那麼inflate()返回的就是“翻譯”得到的view。 
4,無root,同時attachToRoot為true,報錯。 

另外,root還有一個重要的作用就是為“翻譯”得到的view添加合適的LayoutParam,並且如果並不想將得到的View添加到root的話,傳遞何種root是並沒有要求的,比如: 
?
123 View view = mLayoutInflater.inflate(R.layout.header, new ListView(mContext), false);View view = mLayoutInflater.inflate(R.layout.header, new LinearLayout(mContext), false);View view = mLayoutInflater.inflate(R.layout.header, new RelativeLayout(mContext), false);
上面得到的View,除了view的LayoutParam分別為AbsListView.LayoutParams,LinearLayout.LayoutParams,RelativeLayout.LayoutParams之外,內容都一致。 

原 [Android]LIstView的HeaderView

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.