android自定义底部Tab,项目整体界面框架

android自定义底部Tab,项目整体界面框架


共享一个自己在开发过程中搭建的android项目界面框架,便于提高开发效率。


主要功能

1.使用Button自定义底部Tab和Title

2.点击底部Tab后使用Fragment切换页面

3.主页使用ViewPager滚动显示新闻图片

4.自定义类处理Fragment重叠回退问题




一、自定义底部Tab类

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. /******************************************************************************* 
  2.  * 
  3.  * Copyright (c) Weaver Info Tech Co. Ltd 
  4.  * 
  5.  * TabView 
  6.  * 
  7.  * app.ui.widget.TabView.java 
  8.  * TODO: File description or class description. 
  9.  * 
  10.  * @author: Administrator 
  11.  * @changeLogs: 
  12.  *     1.0.0: First created this class. 
  13.  * 
  14.  ******************************************************************************/  
  15. package app.ui.widget;  
  16.   
  17. import mobi.kuaidian.qunakao.R;  
  18. import android.content.Context;  
  19. import android.util.AttributeSet;  
  20. import android.view.View;  
  21. import android.view.View.OnClickListener;  
  22. import android.widget.Button;  
  23. import android.widget.LinearLayout;  
  24.   
  25. /** 
  26.  * @author Administrator 
  27.  * 
  28.  */  
  29. public class TabView extends LinearLayout implements OnClickListener {  
  30.   
  31.     private OnTabChangeListener mOnTabChangedListener;  
  32.     private int mState = 0;  
  33.     private final Button mStateButton1;  
  34.     private final Button mStateButton2;  
  35.     private final Button mStateButton3;  
  36.     private final Button mStateButton4;  
  37.   
  38.     public TabView(Context context) {  
  39.         this(context, null);  
  40.     }  
  41.   
  42.     public TabView(Context context, AttributeSet attrs) {  
  43.         this(context, attrs, 0);  
  44.     }  
  45.   
  46.     /** 
  47.      * @param context 
  48.      * @param attrs 
  49.      * @param defStyle 
  50.      */  
  51.     public TabView(Context context, AttributeSet attrs, int defStyle) {  
  52.         super(context, attrs, defStyle);  
  53.         inflate(context, R.layout.view_tab, this);  
  54.         mStateButton1 = (Button) findViewById(R.id.button_state1);  
  55.         mStateButton2 = (Button) findViewById(R.id.button_state2);  
  56.         mStateButton3 = (Button) findViewById(R.id.button_state3);  
  57.         mStateButton4 = (Button) findViewById(R.id.button_state4);  
  58.   
  59.         mStateButton1.setOnClickListener(this);  
  60.         mStateButton2.setOnClickListener(this);  
  61.         mStateButton3.setOnClickListener(this);  
  62.         mStateButton4.setOnClickListener(this);  
  63.     }  
  64.   
  65.     public void setOnTabChangeListener(OnTabChangeListener listener) {  
  66.         mOnTabChangedListener = listener;  
  67.     }  
  68.   
  69.     public void setCurrentTab(int index) {  
  70.         switchState(index);  
  71.     }  
  72.   
  73.     private void switchState(int state) {  
  74.         if (mState == state) {  
  75.             return;  
  76.         } // else continue  
  77.   
  78.         mState = state;  
  79.         mStateButton1.setSelected(false);  
  80.         mStateButton2.setSelected(false);  
  81.         mStateButton3.setSelected(false);  
  82.         mStateButton4.setSelected(false);  
  83.   
  84.         Object tag = null;  
  85.   
  86.         switch (mState) {  
  87.             case 0:  
  88.                 mStateButton1.setSelected(true);  
  89.                 tag = mStateButton1.getTag();  
  90.                 break;  
  91.   
  92.             case 1:  
  93.                 mStateButton2.setSelected(true);  
  94.                 tag = mStateButton2.getTag();  
  95.                 break;  
  96.   
  97.             case 2:  
  98.                 mStateButton3.setSelected(true);  
  99.                 tag = mStateButton3.getTag();  
  100.                 break;  
  101.   
  102.             case 3:  
  103.                 mStateButton4.setSelected(true);  
  104.                 tag = mStateButton4.getTag();  
  105.                 break;  
  106.   
  107.             default:  
  108.                 break;  
  109.         }  
  110.   
  111.         if (mOnTabChangedListener != null) {  
  112.             if (tag != null && mOnTabChangedListener != null) {  
  113.                 mOnTabChangedListener.onTabChange(tag.toString());  
  114.             } else {  
  115.                 mOnTabChangedListener.onTabChange(null);  
  116.             }  
  117.         } // else ignored  
  118.     }  
  119.   
  120.   
  121.     /* (non-Javadoc) 
  122.      * @see android.view.View.OnClickListener#onClick(android.view.View) 
  123.      */  
  124.     @Override  
  125.     public void onClick(View v) {  
  126.   
  127.         // TODO Auto-generated method stub  
  128.         switch (v.getId()) {  
  129.             case R.id.button_state1:  
  130.                 switchState(0);  
  131.                 break;  
  132.   
  133.             case R.id.button_state2:  
  134.                 switchState(1);  
  135.                 break;  
  136.   
  137.             case R.id.button_state3:  
  138.                 switchState(2);  
  139.                 break;  
  140.   
  141.             case R.id.button_state4:  
  142.                 switchState(3);  
  143.                 break;  
  144.   
  145.             default:  
  146.                 break;  
  147.         }  
  148.     }  
  149.   
  150.     public static interface OnTabChangeListener {  
  151.         public void onTabChange(String tag);  
  152.     }  
  153. }  


二、对应底部Tab的XML布局文件

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <merge xmlns:android="http://schemas.android.com/apk/res/android" >  
  2.   
  3.     <FrameLayout  
  4.         android:layout_width="match_parent"  
  5.         android:layout_height="match_parent"  
  6.         android:layout_weight="1.0" >  
  7.   
  8.         <Button  
  9.             android:id="@+id/button_state1"  
  10.             android:layout_width="match_parent"  
  11.             android:layout_height="match_parent"  
  12.             android:background="@null"  
  13.             android:button="@null"  
  14.             android:drawableTop="@drawable/ic_message_selector"  
  15.             android:gravity="center"  
  16.             android:singleLine="true"  
  17.             android:tag="message"  
  18.             android:text="@string/text_tab_message"  
  19.             android:textColor="@color/text_service_color"  
  20.             android:textSize="14dp" />  
  21.     </FrameLayout>  
  22.   
  23.     <FrameLayout  
  24.         android:layout_width="match_parent"  
  25.         android:layout_height="match_parent"  
  26.         android:layout_weight="1.0" >  
  27.   
  28.         <Button  
  29.             android:id="@+id/button_state2"  
  30.             android:layout_width="match_parent"  
  31.             android:layout_height="match_parent"  
  32.             android:background="@null"  
  33.             android:button="@null"  
  34.             android:drawableTop="@drawable/ic_service_selector"  
  35.             android:gravity="center"  
  36.             android:singleLine="true"  
  37.             android:tag="service"  
  38.             android:text="@string/text_tab_service"  
  39.             android:textColor="@color/text_service_color"  
  40.             android:textSize="14dp" />  
  41.     </FrameLayout>  
  42.   
  43.     <FrameLayout  
  44.         android:layout_width="match_parent"  
  45.         android:layout_height="match_parent"  
  46.         android:layout_weight="1.0" >  
  47.   
  48.         <Button  
  49.             android:id="@+id/button_state3"  
  50.             android:layout_width="match_parent"  
  51.             android:layout_height="match_parent"  
  52.             android:background="@null"  
  53.             android:button="@null"  
  54.             android:drawableTop="@drawable/ic_profile_selector"  
  55.             android:gravity="center"  
  56.             android:singleLine="true"  
  57.             android:tag="personal"  
  58.             android:text="@string/text_tab_profile"  
  59.             android:textColor="@color/text_service_color"  
  60.             android:textSize="14dp" />  
  61.     </FrameLayout>  
  62.   
  63.     <FrameLayout  
  64.         android:layout_width="match_parent"  
  65.         android:layout_height="match_parent"  
  66.         android:layout_weight="1.0" >  
  67.   
  68.         <Button  
  69.             android:id="@+id/button_state4"  
  70.             android:layout_width="match_parent"  
  71.             android:layout_height="match_parent"  
  72.             android:background="@null"  
  73.             android:button="@null"  
  74.             android:drawableTop="@drawable/ic_setting_selector"  
  75.             android:gravity="center"  
  76.             android:singleLine="true"  
  77.             android:tag="settings"  
  78.             android:text="@string/text_tab_setting"  
  79.             android:textColor="@color/text_service_color"  
  80.             android:textSize="14dp" />  
  81.     </FrameLayout>  
  82.   
  83. </merge>  



三、在启动的Acitvity界面中使用自定义的Tab类

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  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:orientation="vertical" >  
  6.   
  7.     <include layout="@layout/layout_titlebar" />  
  8.   
  9.     <FrameLayout  
  10.         android:id="@+id/layout_content"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="match_parent"  
  13.         android:layout_weight="1.0" />  
  14.   
  15.     <app.ui.widget.TabView  
  16.         android:id="@+id/view_tab"  
  17.         android:layout_width="match_parent"  
  18.         android:layout_height="@dimen/header_height"  
  19.         android:background="@color/tab_main_color" />  
  20.   
  21. </LinearLayout>  



四、自定义类处理Fragment返回重叠的问题

[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. /******************************************************************************* 
  2.  * 
  3.  * Copyright (c) Baina Info Tech Co. Ltd 
  4.  * 
  5.  * FragmentUtils 
  6.  * 
  7.  * app.util.FragmentUtils.java 
  8.  * TODO: File description or class description. 
  9.  * 
  10.  * @changeLogs: 
  11.  *     1.0.0: First created this class. 
  12.  * 
  13.  ******************************************************************************/  
  14. package app.util;  
  15.   
  16. import android.os.Bundle;  
  17. import android.support.v4.app.Fragment;  
  18. import android.support.v4.app.FragmentManager;  
  19. import android.support.v4.app.FragmentTransaction;  
  20.   
  21. /** 
  22.  * FragmentUtils of MyHealth 
  23.  * 
  24.  * @author qixiao 
  25.  */  
  26. public class FragmentUtils {  
  27.   
  28.     private FragmentUtils() {  
  29.   
  30.     }  
  31.   
  32.     public static Fragment replaceFragment(FragmentManager fragmentManager, int container,  
  33.             Class<? extends Fragment> newFragment, Bundle args) {  
  34.         return replaceFragment(fragmentManager, container, newFragment, args, false);  
  35.     }  
  36.   
  37.     public static Fragment replaceFragment(FragmentManager fragmentManager, int container,  
  38.             Fragment newFragment) {  
  39.         return replaceFragment(fragmentManager, container, newFragment, false);  
  40.     }  
  41.   
  42.     public static Fragment replaceFragment(FragmentManager fragmentManager, int container,  
  43.             Class<? extends Fragment> newFragment, Bundle args, boolean addToBackStack) {  
  44.   
  45.         Fragment fragment = null;  
  46.   
  47.         // 构造新的Fragment  
  48.         try {  
  49.             fragment = newFragment.newInstance();  
  50.         } catch (InstantiationException e) {  
  51.             e.printStackTrace();  
  52.         } catch (IllegalAccessException e) {  
  53.             e.printStackTrace();  
  54.         }  
  55.   
  56.         if (fragment != null) {  
  57.             // 设置参数  
  58.             if (args != null && !args.isEmpty()) {  
  59.                 final Bundle bundle = fragment.getArguments();  
  60.                 if (bundle != null) {  
  61.                     bundle.putAll(args);  
  62.                 } else {  
  63.                     fragment.setArguments(args);  
  64.                 }  
  65.             }  
  66.             // 替换  
  67.             return replaceFragment(fragmentManager, container, fragment, addToBackStack);  
  68.         } else {  
  69.             return null;  
  70.         }  
  71.     }  
  72.   
  73.     public static Fragment replaceFragment(FragmentManager fragmentManager, int container,  
  74.             Fragment newFragment, boolean addToBackStack) {  
  75.         final FragmentTransaction transaction = fragmentManager.beginTransaction();  
  76.         final String tag = newFragment.getClass().getSimpleName();  
  77.   
  78.         if (newFragment != null) {  
  79.             transaction.replace(container, newFragment, tag);  
  80.         }  
  81.   
  82.         if (addToBackStack) {  
  83.             transaction.addToBackStack(null);  
  84.         }  
  85.         transaction.commitAllowingStateLoss();  
  86.         return newFragment;  
  87.     }  
  88.   
  89.     public static Fragment switchFragment(FragmentManager fragmentManager, int container,  
  90.             Fragment currentFragment, Class<? extends Fragment> newFragment, Bundle args) {  
  91.         return switchFragment(fragmentManager, container, currentFragment, newFragment, args, false);  
  92.     }  
  93.   
  94.     /** 
  95.      * 
  96.      * @param fragmentManager 
  97.      * @param container 
  98.      * @param currentFragment 
  99.      * @param newFragment 
  100.      * @param args 新Fragment的参数 
  101.      * @param addToBackStack 这个操作是否加入栈中,如果要实现类似返回效果,则需要。 
  102.      * @return 新显示的Fragment 
  103.      */  
  104.     public static Fragment switchFragment(FragmentManager fragmentManager, int container,  
  105.             Fragment currentFragment, Class<? extends Fragment> newFragment, Bundle args,  
  106.             boolean addToBackStack) {  
  107.   
  108.         final FragmentTransaction transaction = fragmentManager.beginTransaction();  
  109.         final String tag = newFragment.getSimpleName();  
  110.         Fragment fragment = fragmentManager.findFragmentByTag(tag);  
  111.   
  112.         // 如果在栈中找到相应的Fragment,则显示,否则重新生成一个  
  113.         if (fragment != null) {  
  114.             if (fragment != currentFragment) {  
  115.                 if (currentFragment != null) {  
  116.                     transaction.hide(currentFragment);  
  117.                 }  
  118.                 transaction.show(fragment);  
  119.                 if (addToBackStack) {  
  120.                     transaction.addToBackStack(null);  
  121.                 }  
  122.                 transaction.commitAllowingStateLoss();  
  123.             } else {  
  124.                 fragment.getArguments().putAll(args);  
  125.             }  
  126.   
  127.             return fragment;  
  128.         } else {  
  129.             try {  
  130.                 fragment = newFragment.newInstance();  
  131.             } catch (InstantiationException e) {  
  132.                 e.printStackTrace();  
  133.             } catch (IllegalAccessException e) {  
  134.                 e.printStackTrace();  
  135.             }  
  136.         }  
  137.   
  138.         // 为新的Fragment添加参数  
  139.         if (fragment != null) {  
  140.             if (args != null && !args.isEmpty()) {  
  141.                 final Bundle bundle = fragment.getArguments();  
  142.                 if (bundle != null) {  
  143.                     bundle.putAll(args);  
  144.                 } else {  
  145.                     fragment.setArguments(args);  
  146.                 }  
  147.             }  
  148.         }  
  149.   
  150.         // 显示新的Fragment  
  151.         if (currentFragment != null) {  
  152.             transaction.hide(currentFragment);  
  153.         }  
  154.         transaction.add(container, fragment, tag);  
  155.         if (addToBackStack) {  
  156.             transaction.addToBackStack(null);  
  157.         }  
  158.         transaction.commitAllowingStateLoss();  
  159.   
  160.         return fragment;  
  161.     }  
  162.   
  163. }  


源码连接地址:http://download.csdn.net/detail/gao_chun/7645909




五、自定义控件系列

Android项目中使用自定义进度加载Dialog:http://blog.csdn.net/gao_chun/article/details/45270031

ListView中按钮监听器 设置 及 优化http://blog.csdn.net/gao_chun/article/details/41249131

Android项目中自定义顶部标题栏:http://blog.csdn.net/gao_chun/article/details/45255929

Android自定义设置圆形图片控件http://blog.csdn.net/gao_chun/article/details/39207557

自定义AlertDialog提示框http://blog.csdn.net/gao_chun/article/details/37757571

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值