Android Fragment 真正的完全解析(下)

来源:互联网 发布:西部证券软件 编辑:程序博客网 时间:2024/05/16 19:55

在android的开发过程中经常会遇到页面切换的问题,其中一个解决办法是使用fragment加Handler来实现,不过有些情况下这种方法并不是最好的选择。比如,你需要滑动切换页面的时候。这时使用TabHost和ViewPager来实现会更加方便。文章参考API文档中Creating Swipe Views with Tabs(文章路径Training->Implementing Effective Navigation->Creating Swipe Views with Tabs)和west8623的文章。并且加入了自己定义的标题。


代码如下:

第一步,建立mypage_layout.xml

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <TabHost  
  2.         android:id="@+id/mypage_tabhost"  
  3.         android:layout_width="match_parent"  
  4.         android:layout_height="match_parent"  
  5.         android:layout_below="@+id/mypage_r0">  
  6.         <LinearLayout  
  7.             android:orientation="vertical"  
  8.             android:layout_width="match_parent"  
  9.             android:layout_height="match_parent">  
  10.             <TabWidget  
  11.                 android:id="@android:id/tabs"  
  12.                 android:orientation="horizontal"  
  13.                 android:layout_width="match_parent"  
  14.                 android:layout_height="wrap_content"  
  15.                 android:layout_weight="0"/>  
  16.             <FrameLayout  
  17.                 android:id="@android:id/tabcontent"  
  18.                 android:layout_width="0dp"  
  19.                 android:layout_height="0dip"  
  20.                 android:layout_weight="0"/>  
  21.             <android.support.v4.view.ViewPager  
  22.                 android:id="@+id/mypage_pager"  
  23.                 android:layout_width="match_parent"  
  24.                 android:layout_height="0dip"  
  25.                 android:layout_weight="1"/>  
  26.         </LinearLayout>  
  27.     </TabHost>  


第二步:为每个子页面建立xml,如fragment_mypage.xml;


[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text"   
  3. android:layout_width="match_parent" android:layout_height="match_parent"   
  4. android:gravity="center_vertical|center_horizontal"   
  5. android:textAppearance="?android:attr/textAppearanceMedium"   
  6. android:text="@string/hello_world"/>  


第三步:为tabHost建立自定义标签tabwidget_layout.xml:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:background="#f5f5f5">  
  6.     <TextView  
  7.         android:id="@+id/tabwidget_tv"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:textSize="14sp"  
  11.         android:text="原创"  
  12.         android:textColor="#333333"  
  13.         android:layout_centerHorizontal="true"  
  14.         android:layout_alignParentTop="true"  
  15.         android:layout_marginTop="8dip"/>  
  16.     <View  
  17.         android:id="@+id/tabwidget_line"  
  18.         android:layout_width="match_parent"  
  19.         android:layout_height="1dip"  
  20.         android:background="#d0d0d0"  
  21.         android:layout_below="@+id/tabwidget_tv"  
  22.         android:layout_marginTop="8dip"/>  
  23. </RelativeLayout>  


第四步:写主页面MyPageActivity.java,同时要建立子页面FragmentMyPager.java

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class FragmentMyPager extends Fragment  
  2. {  
  3.     int mNum;  
  4.   
  5.     public static FragmentMyPager newInstance(int num)  
  6.     {  
  7.         FragmentMyPager f=new FragmentMyPager();  
  8.   
  9.         Bundle args=new Bundle();  
  10.         args.putInt("num",num);  
  11.         f.setArguments(args);  
  12.         return f;  
  13.     }  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState)  
  16.     {  
  17.         super.onCreate(savedInstanceState);  
  18.         mNum=getArguments()!=null?getArguments().getInt("num"):1;  
  19.     }  
  20.   
  21.     @Override  
  22.     public View onCreateView(LayoutInflater inflater,ViewGroup container,  
  23.                              Bundle savedInstanceState)  
  24.     {  
  25.         View v=inflater.inflate(R.layout.fragment_mypage,container,false);  
  26.         View tv=v.findViewById(R.id.text);  
  27.         ((TextView)tv).setText("Fragment # "+mNum);  
  28.         ((TextView)tv).setTextColor(getResources().getColor(R.color.black));  
  29.         return v;  
  30.     }  
  31. }  
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class MyPageActivity extends FragmentActivity  
  2. {  
  3.     private TabHost mTabHost;  
  4.     private ViewPager mViewPager;  
  5.     private TabsAdapter mTabsAdapter;  
  6.     private TextView tabTv1,tabTv2;  
  7.     private View tabLine1,tabLine2,view1,view2;  
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState)  
  10.     {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.mypage_layout);  
  13.         mTabHost=(TabHost)findViewById(R.id.mypage_tabhost);  
  14.         mTabHost.setup();  
  15.   
  16.         mViewPager=(ViewPager)findViewById(R.id.mypage_pager);  
  17.         mTabsAdapter=new TabsAdapter(this,mTabHost,mViewPager);  
  18.   
  19.         view1=(View) LayoutInflater.from(this).inflate(R.layout.tabwidget_layout,null);  
  20.         tabTv1=(TextView)view1.findViewById(R.id.tabwidget_tv);  
  21.         tabLine1=(View)view1.findViewById(R.id.tabwidget_line);  
  22.   
  23.         view2=(View) LayoutInflater.from(this).inflate(R.layout.tabwidget_layout,null);  
  24.         tabTv2=(TextView)view2.findViewById(R.id.tabwidget_tv);  
  25.         tabLine2=(View)view2.findViewById(R.id.tabwidget_line);  
  26.   
  27.         tabTv1.setText("原创");  
  28.         tabTv1.setTextColor(getResources().getColor(R.color.orange));  
  29.         tabLine1.setBackgroundColor(getResources().getColor(R.color.orange));  
  30.   
  31.         tabTv2.setText("赞过");  
  32.   
  33.         mTabsAdapter.addTab(mTabHost.newTabSpec("simple").setIndicator(view1),  
  34.                 FragmentMyPager.class,null);  
  35.         mTabsAdapter.addTab(mTabHost.newTabSpec("contacts").setIndicator(view2),  
  36.                 FragmentMyPager.class,null);  
  37.   
  38.         if(savedInstanceState!=null)  
  39.         {  
  40.             mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));  
  41.         }  
  42.     }  
  43.   
  44.     @Override  
  45.     protected void onSaveInstanceState(Bundle outState)  
  46.     {  
  47.         super.onSaveInstanceState(outState);  
  48.         outState.putString("tab",mTabHost.getCurrentTabTag());  
  49.     }  
  50.   
  51.     private class TabInfo  
  52.     {  
  53.         private String tag;  
  54.         private Class<?> clss;  
  55.         private Bundle args;  
  56.   
  57.         TabInfo(String _tag,Class<?> _class,Bundle _args)  
  58.         {  
  59.             tag=_tag;  
  60.             clss=_class;  
  61.             args=_args;  
  62.         }  
  63.     }  
  64.     private class DummyTabFactory implements TabHost.TabContentFactory  
  65.     {  
  66.         private Context mContext;  
  67.   
  68.         public DummyTabFactory(Context context)  
  69.         {  
  70.             mContext=context;  
  71.         }  
  72.   
  73.         @Override  
  74.         public View createTabContent(String tag)  
  75.         {  
  76.             View v=new View(mContext);  
  77.             v.setMinimumHeight(0);  
  78.             v.setMinimumWidth(0);  
  79.             return v;  
  80.         }  
  81.     }  
  82.     private class TabsAdapter extends FragmentPagerAdapter  
  83.                 implements TabHost.OnTabChangeListener,ViewPager.OnPageChangeListener  
  84.     {  
  85.         private Context mContext;  
  86.         private TabHost mTabHost;  
  87.         private ViewPager mViewPager;  
  88.         private ArrayList<TabInfo> mTabs=new ArrayList<TabInfo>();  
  89.   
  90.         public TabsAdapter(FragmentActivity activity,TabHost tabHost,ViewPager pager)  
  91.         {  
  92.             super(activity.getSupportFragmentManager());  
  93.             mContext=activity;  
  94.             mTabHost=tabHost;  
  95.             mViewPager=pager;  
  96.             mTabHost.setOnTabChangedListener(this);  
  97.             mViewPager.setAdapter(this);  
  98.             mViewPager.setOnPageChangeListener(this);  
  99.         }  
  100.   
  101.         public void addTab(TabHost.TabSpec tabSpec,Class<?> clss,Bundle args)  
  102.         {  
  103.             tabSpec.setContent(new DummyTabFactory(mContext));  
  104.             String tag=tabSpec.getTag();  
  105.   
  106.             TabInfo info=new TabInfo(tag,clss,args);  
  107.             mTabs.add(info);  
  108.             mTabHost.addTab(tabSpec);  
  109.             notifyDataSetChanged();  
  110.         }  
  111.   
  112.         @Override  
  113.         public int getCount()  
  114.         {  
  115.             return mTabs.size();  
  116.         }  
  117.   
  118.         @Override  
  119.         public Fragment getItem(int position)  
  120.         {  
  121.             TabInfo info=mTabs.get(position);  
  122.             return Fragment.instantiate(mContext,info.clss.getName(),info.args);  
  123.         }  
  124.   
  125.         @Override  
  126.         public void onTabChanged(String tabId)  
  127.         {  
  128.             int position=mTabHost.getCurrentTab();  
  129.             mViewPager.setCurrentItem(position);  
  130.             if(position==0)  
  131.             {  
  132.                 tabTv1.setText("原创");  
  133.                 tabTv1.setTextColor(getResources().getColor(R.color.orange));  
  134.                 tabLine1.setBackgroundColor(getResources().getColor(R.color.orange));  
  135.   
  136.                 tabTv2.setText("赞过");  
  137.                 tabTv2.setTextColor(getResources().getColor(R.color.black));  
  138.                 tabLine2.setBackgroundColor(getResources().getColor(R.color.linebg));  
  139.             }else if(position==1)  
  140.             {  
  141.                 tabTv2.setText("赞过");  
  142.                 tabTv2.setTextColor(getResources().getColor(R.color.orange));  
  143.                 tabLine2.setBackgroundColor(getResources().getColor(R.color.orange));  
  144.   
  145.                 tabTv1.setText("原创");  
  146.                 tabTv1.setTextColor(getResources().getColor(R.color.black));  
  147.                 tabLine1.setBackgroundColor(getResources().getColor(R.color.linebg));  
  148.             }  
  149. //            String tmp=mTabHost.getCurrentTabTag();  
  150. //            mTabHost.getCurrentTabView().set  
  151.         }  
  152.   
  153.         @Override  
  154.         public void onPageScrolled(int position,float positionOffset,int positionOffsetPixels)  
  155.         {  
  156.         }  
  157.   
  158.         @Override  
  159.         public void onPageSelected(int position)  
  160.         {  
  161.             TabWidget widget=mTabHost.getTabWidget();  
  162.             int oldFocusability=widget.getDescendantFocusability();  
  163.             widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);  
  164.             mTabHost.setCurrentTab(position);  
  165.             widget.setDescendantFocusability(oldFocusability);  
  166.         }  
  167.         @Override  
  168.         public void onPageScrollStateChanged(int state)  
  169.         {  
  170.   
  171.         }  
  172.     }  
  173. }  




0 0