关于ActionBar Tab与ListFragment的结合

来源:互联网 发布:百度彩票数据接口 编辑:程序博客网 时间:2024/04/30 02:50

 <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中

  @Overridepublic 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的效果了!

0 0