2013-12-10 38 views
1

我使用以下佈局(header.xml)在列表視圖中添加標題,addHeaderView不起作用

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/greetingContainer" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

<TextView 
    android:id="@+id/categoryTitle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="header view" 
    android:textColor="#FFFFFF" 
    android:textSize="15sp" /> 

</LinearLayout> 

,並在另一邊,我使用,

View header = (View) getLayoutInflater().inflate(R.layout.header,null); 
getListView().addHeaderView(header); 

當列表爲空它不工作,保持無形..

我的問題可能是this重複,但無法理解,
請幫忙!

+0

您可以在文本視圖的頂部和列表視圖中使用相對佈局的文本視圖。 yes listview爲空時不會顯示 – Raghunandan

+0

當列表包含數據時,一切正常! –

+0

是的,它意味着默認情況下這種方式工作。 – Raghunandan

回答

0

我已經使用了一個類似的標題視圖,當列表視圖中沒有數據時它會很好。

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    view = inflater.inflate(R.layout.list, null); 
    listView = (ListView) view.findViewById(R.id.listView1); 

    if (listView.getHeaderViewsCount() == 0) { 
     headerView = View.inflate(getActivity(), R.layout.listheader,  null); 
     listView.addHeaderView(headerView, null, false); 
    } 
    return view; 

} 
3

簡單的解決方案,所有你需要做的是,你應該始終設置適配器爲您listview,即使你沒有任何數據顯示。

+1

這對我工作,應該可能被標記爲正確答案。只需設置一個帶有空數據集的「ArrayAdapter」。 – infl3x