關於ActionBar Tab與ListFragment的結合

 <FrameLayout
        android:id="@+id/main_titles"
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1" />
Actionbar繼承Activity即可

通過複寫onTabSelected方法可使每個Tab對應到相應的fragment

</pre><pre name="code" class="java">public void onTabSelected(Tab tab, FragmentTransaction ft) {
			
			
			ft.replace(R.id.main_titles, fragment, null); }//用相應的Fragment填充FrameLayout


Fragment.java繼承ListFragment來實現列表效果,通過複寫oncreatview將listview填充進上述FrameLayout中

	  @Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
	    	return inflater.inflate(R.layout.fragment_list, container,false);
		}
下面是fragment_list.xml的代碼,一定要有Listview,一定要有<span style="font-family: Arial, Helvetica, sans-serif;">android:id="@id/android:list"</span>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
      
    <!-- listfragment 一定要從android:list中啓動 -->
    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        
      
       />

</LinearLayout>
在public void onActivityCreated(Bundle savedInstanceState)中設置listadapter

setListAdapter(simpleAdapter);

各種adapter,如simpleadapter,arrayadapter,等等都可以放入setListAdapter,

這樣就可以實現actionbar Tab中放入listFragment的效果了!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章