关于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的效果了!

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