Fragment(二)

前端之家收集整理的这篇文章主要介绍了Fragment(二)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1、先建立一个类(IconFramgnetBean),在里面定义出需要的图片标题,所需要的类(Class

IconFramgnetBean

  1. public class IconFramgnetBean {
  2. private int iconResId;
  3. private String title;
  4. private Class<?> clazz;
  5.  
  6. public IconFramgnetBean(int iconResId,Class<?> clazz) {
  7. this.iconResId = iconResId;
  8. this.clazz = clazz;
  9. }
  10.  
  11. public IconFramgnetBean(int iconResId,String title,Class<?> clazz) {
  12. this.iconResId = iconResId;
  13. this.title = title;
  14. this.clazz = clazz;
  15. }
  16.  
  17. public int getIconResId() {
  18. return iconResId;
  19. }
  20.  
  21. public void setIconResId(int iconResId) {
  22. this.iconResId = iconResId;
  23. }
  24.  
  25. public String getTitle() {
  26. return title;
  27. }
  28.  
  29. public void setTitle(String title) {
  30. this.title = title;
  31. }
  32.  
  33. public Class<?> getClazz() {
  34. return clazz;
  35. }
  36.  
  37. public void setClazz(Class<?> clazz) {
  38. this.clazz = clazz;
  39. }
  40.  
  41. }

2、建立所需的Fragment:如下

建立所需的Fragment

FragmentHome:

  1. public class FragmentHome extends Fragment {
  2. public static final int RESID = R.drawable.tab_home;
  3. public static final String TITLE = "首页";
  4.  
  5. @Override
  6. public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
  7. View view = inflater.inflate(R.layout.fragment_home,container,false);
  8. return view;
  9. }
  10. }

FragmentSay :

  1. public class FragmentSay extends Fragment {
  2. public static final int RESID = R.drawable.tab_say;
  3. public static final String TITLE = "咨询";
  4.  
  5. @Override
  6. public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) {
  7. View view = inflater.inflate(R.layout.fragment_say,null);
  8. return view;
  9. }
  10. }

FragmentShopCar :

  1. public class FragmentShopCar extends Fragment {
  2. public static final int RESID = R.drawable.tab_shopcar;
  3. public static final String TITLE = "购物车";
  4.  
  5. @Override
  6. public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) {
  7. View view = inflater.inflate(R.layout.fragment_shopcar,null);
  8. return view;
  9. }
  10.  
  11. }

FragmentMy :

  1. public class FragmentMy extends Fragment {
  2. public static final int RESID = R.drawable.tab_my;
  3. public static final String TITLE = "我的";
  4.  
  5. @Override
  6. public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) {
  7. View view = inflater.inflate(R.layout.fragment_my,null);
  8. return view;
  9. }
  10.  
  11. }

XML文件

fragment_home.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:gravity="center"
  6. android:orientation="vertical" >
  7. <ImageView
  8. android:id="@+id/fragment_first"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. android:src="@drawable/home"/>
  12.  
  13. </LinearLayout>

fragment_say.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:gravity="center"
  6. android:orientation="vertical" >
  7.  
  8. <ImageView
  9. android:id="@+id/fragment_second"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. android:src="@drawable/say"/>
  13. </LinearLayout>

fragment_shopcar.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:gravity="center"
  6. android:orientation="vertical" >
  7.  
  8. <ImageView
  9. android:id="@+id/fragment_third"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. android:src="@drawable/shopcar"
  13. />
  14. </LinearLayout>

fragment_my.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:gravity="center"
  6. android:orientation="vertical" >
  7.  
  8. <ImageView
  9. android:id="@+id/fragment_fourth"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. android:src="@drawable/my"
  13. />
  14. </LinearLayout>

activity_main.xml:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="fill_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6.  
  7. <FrameLayout
  8. android:id="@+id/realTabContent"
  9. android:layout_width="fill_parent"
  10. android:layout_height="0dip"
  11. android:layout_weight="1" />
  12.  
  13. <android.support.v4.app.FragmentTabHost
  14. android:id="@android:id/tabhost"
  15. android:layout_width="fill_parent"
  16. android:layout_height="50dip"
  17. android:background="@null" >
  18.  
  19. <FrameLayout
  20. android:id="@android:id/tabcontent"
  21. android:layout_width="0dp"
  22. android:layout_height="0dp"
  23. android:layout_weight="0" />
  24. </android.support.v4.app.FragmentTabHost>
  25.  
  26. </LinearLayout>

MainActivity

  1. public class MainActivity extends FragmentActivity {
  2.  
  3. private List<IconFramgnetBean> beans;
  4. private FragmentTabHost mTabHost;
  5.  
  6. private Activity getSelf() {
  7. return MainActivity.this;
  8. }
  9.  
  10. @Override
  11. protected void onCreate(Bundle arg0) {
  12. super.onCreate(arg0);
  13. beans = InitConfig.initIconFragmentBeans();
  14. requestWindowFeature(Window.FEATURE_NO_TITLE);
  15. setContentView(R.layout.activity_main);
  16. mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
  17. mTabHost.setup(getSelf(),getSupportFragmentManager(),R.id.realTabContent);
  18. mTabHost.getTabWidget().setDividerDrawable(null);
  19. // 得到fragment的个数
  20. if (beans != null && !beans.isEmpty()) {
  21. int count = beans.size();
  22. for (int i = 0; i < count; i++) {
  23. IconFramgnetBean iconTextBean = beans.get(i);
  24. TabSpec tabSpec = mTabHost.newTabSpec(iconTextBean.getTitle()).setIndicator(getTabItemView(LayoutInflater.from(getSelf()),iconTextBean));
  25. mTabHost.addTab(tabSpec,iconTextBean.getClazz(),null);
  26. }
  27. mTabHost.setCurrentTab(0);
  28. mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
  29. @Override
  30. public void onTabChanged(String tabId) {
  31. mTabHost.setCurrentTabByTag(tabId);
  32. }
  33. });
  34. }
  35.  
  36. }
  37.  
  38. /** * 给Tab按钮设置图标和文字 */
  39. private View getTabItemView(LayoutInflater inflater,IconFramgnetBean iconTextBean) {
  40. ImageView imageView = new ImageView(getSelf());
  41. imageView.setBackgroundResource(iconTextBean.getIconResId());
  42. return imageView;
  43. }
  44.  
  45.  
  46. boolean bExit = true;
  47.  
  48. @Override
  49. public boolean onKeyDown(int keyCode,KeyEvent event) {
  50. if (keyCode == KeyEvent.KEYCODE_BACK) {
  51. if (bExit) {
  52. final Timer TTimer = new Timer();
  53. TTimer.schedule(new TimerTask() {
  54. @Override
  55. public void run() {
  56. TTimer.cancel();
  57. bExit = true;
  58. }
  59. },1000);
  60. Toast.makeText(getSelf(),"再按一次退出",Toast.LENGTH_SHORT).show();
  61. bExit = false;
  62. } else {
  63. getSelf().finish();
  64. }
  65. }
  66. return false;
  67. }
  68.  
  69. }

InitConfig

  1. public class InitConfig {
  2. public static List<IconFramgnetBean> initIconFragmentBeans() {
  3. List<IconFramgnetBean> beans = new ArrayList<IconFramgnetBean>();
  4. beans.add(new IconFramgnetBean(FragmentHome.RESID,FragmentHome.TITLE,FragmentHome.class));
  5. beans.add(new IconFramgnetBean(FragmentSay.RESID,FragmentSay.TITLE,FragmentSay.class));
  6. beans.add(new IconFramgnetBean(FragmentShopCar.RESID,FragmentShopCar.TITLE,FragmentShopCar.class));
  7. beans.add(new IconFramgnetBean(FragmentMy.RESID,FragmentMy.TITLE,FragmentMy.class));
  8. return beans;
  9. }
  10. }

drawable.tab_home

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:drawable="@drawable/shouyeanxia" android:state_selected="true"/>
  4. <item android:drawable="@drawable/shouyeanxia" android:state_pressed="true"/>
  5. <item android:drawable="@drawable/shouye"/>
  6. </selector>

猜你在找的XML相关文章