上一节介绍了TabActivity的简单用法,但是现在的Api中已经不建议使用了,建议使用Fragment来替代以上的功能,下面介绍下使用Fragment和ViewPager的结合使用。
http://blog.csdn.net/xia215266092/article/details/9613985
TabActivity的文档。
This class was deprecated in API level 13.
New applications should use Fragments instead of this class; to continue to run on older devices, you can use the v4 support
library which provides a version of the Fragment API that is compatible down to DONUT
.
下面的示例是使用android-support-v4.jar,写的demo,这个demo也是api的demo。
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TabWidget android:id="@android:id/tabs" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0"/> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0"/> <FrameLayout android:id="@+id/realtabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> </LinearLayout> </android.support.v4.app.FragmentTabHost>
import com.example.android.supportv4.R; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTabHost; /** * This demonstrates how you can implement switching between the tabs of a * TabHost through fragments, using FragmentTabHost. */ public class FragmentTabs extends FragmentActivity { private FragmentTabHost mTabHost; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_tabs); mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost); mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"), FragmentStackSupport.CountingFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"), LoaderCursorSupport.CursorLoaderListFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"), LoaderCustomSupport.AppListFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"), LoaderThrottleSupport.ThrottledLoaderListFragment.class, null); } }
但是今天讲的不是这个,我讲Fragment是Android4.0以后的Fragment,android.app.Fragment,v4扩展包里面也有Fragment,但是是不一样的。
下面是用的是4.0以后的fragment,编译的版本和手机必须是4.0以后的,还是我这边引用的是android-support-v13.jar,这个jar,不是v4的。
package org.liushui.tabdemo.frag; import java.util.ArrayList; import java.util.List; import org.liushui.tabdemo.R; import android.app.ActionBar; import android.app.ActionBar.Tab; import android.app.Activity; import android.app.Fragment; import android.app.FragmentTransaction; import android.content.Context; import android.os.Bundle; import android.support.v13.app.FragmentStatePagerAdapter; import android.support.v4.view.ViewPager; public class MainFragmentActivity extends Activity { private ViewPager mViewPager; private TabsAdapter mTabsAdapter; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.frag_main); mViewPager = (ViewPager) findViewById(R.id.pager); ActionBar bar = getActionBar(); bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME); mTabsAdapter = new TabsAdapter(this, mViewPager); mTabsAdapter.addTab(bar.newTab().setText("电话"), AFragment.class, null); mTabsAdapter.addTab(bar.newTab().setText("短信"), BFragment.class, null); mTabsAdapter.addTab(bar.newTab().setText("拨号"), CFragment.class, null); } static class TabsAdapter extends FragmentStatePagerAdapter implements ActionBar.TabListener, ViewPager.OnPageChangeListener { private Context mContext; private ActionBar mActionBar; private ViewPager mViewPager; private List<TabInfo> mTabs = new ArrayList<TabInfo>(); static class TabInfo { private Class<?> clss; private Bundle args; private Fragment fragment; TabInfo(Class<?> _class, Bundle _args) { clss = _class; args = _args; } } public TabsAdapter(Activity activity, ViewPager pager) { super(activity.getFragmentManager()); mContext = activity; mActionBar = activity.getActionBar(); mViewPager = pager; mViewPager.setAdapter(this); mViewPager.setOnPageChangeListener(this); mViewPager.setOffscreenPageLimit(3); } public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) { TabInfo info = new TabInfo(clss, args); tab.setTag(info); tab.setTabListener(this); mTabs.add(info); mActionBar.addTab(tab); } public int getCount() { return mTabs.size(); } public int getItemPosition(Object object) { return POSITION_NONE; } public Fragment getItem(int position) { TabInfo info = mTabs.get(position); if (info.fragment == null) { info.fragment = Fragment.instantiate(mContext, info.clss.getName(), info.args); } return info.fragment; } public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } public void onPageSelected(int position) { mActionBar.setSelectedNavigationItem(position); } public void onPageScrollStateChanged(int state) { } public void onTabSelected(Tab tab, FragmentTransaction ft) { mViewPager.setCurrentItem(tab.getPosition(), true); } public void onTabUnselected(Tab tab, FragmentTransaction ft) { } public void onTabReselected(Tab tab, FragmentTransaction ft) { } } }
布局文件是很简单的,就是一个ViewPager就可以了,关于ViewPager,有点类似Android手机上面的Launcher,可以左右滑动。
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.ViewPager>上面代码中的子页面,AFragment,BFragment,CFragment,都是继承Fragment的,注意使用的android.app.Fragment.
package org.liushui.tabdemo.frag; import org.liushui.tabdemo.R; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * Title: AFragment.java<br> * author:xiaqiulei@gmail.com <br> * Date: 2013年7月29日<br> * Version:v1.0 */ public class AFragment extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.a, container, false); return view; } }onCreateView这个方法,就是返回一个界面对象,在ViewPager中显示。