此版本在“一个不错的UI框架”基础上修改了首次启动底部导航没有选中Tab的情况
运行效果图如下:
1、TabView.java 这里修改的是mState=-1
/********************************************************************************* Copyright (c) Weaver Info Tech Co. Ltd** TabView** app.ui.widget.TabView.java* TODO: File description or class description.** @author: Administrator* @since: 2014-4-22* @version: 1.0.0** @changeLogs:* 1.0.0: First created this class.*******************************************************************************/ package app.ui.widget;import mobi.kuaidian.qunakao.R; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout;/*** @author Administrator**/ public class TabView extends LinearLayout implements OnClickListener {private OnTabChangeListener mOnTabChangedListener;private int mState = -1;private final Button mStateButton1;private final Button mStateButton2;private final Button mStateButton3;private final Button mStateButton4;public TabView(Context context) {this(context, null);}public TabView(Context context, AttributeSet attrs) {this(context, attrs, 0);}/*** @param context* @param attrs* @param defStyle*/public TabView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);inflate(context, R.layout.view_tab, this);mStateButton1 = (Button) findViewById(R.id.button_state1);mStateButton2 = (Button) findViewById(R.id.button_state2);mStateButton3 = (Button) findViewById(R.id.button_state3);mStateButton4 = (Button) findViewById(R.id.button_state4);mStateButton1.setOnClickListener(this);mStateButton2.setOnClickListener(this);mStateButton3.setOnClickListener(this);mStateButton4.setOnClickListener(this);}public void setOnTabChangeListener(OnTabChangeListener listener) {mOnTabChangedListener = listener;}public void setCurrentTab(int index) {switchState(index);}private void switchState(int state) {if (mState == state) {return;} // else continue mState = state;mStateButton1.setSelected(false);mStateButton2.setSelected(false);mStateButton3.setSelected(false);mStateButton4.setSelected(false);Object tag = null;switch (mState) {case 0:mStateButton1.setSelected(true);tag = mStateButton1.getTag();break;case 1:mStateButton2.setSelected(true);tag = mStateButton2.getTag();break;case 2:mStateButton3.setSelected(true);tag = mStateButton3.getTag();break;case 3:mStateButton4.setSelected(true);tag = mStateButton4.getTag();break;default:break;}if (mOnTabChangedListener != null) {if (tag != null && mOnTabChangedListener != null) {mOnTabChangedListener.onTabChange(tag.toString());} else {mOnTabChangedListener.onTabChange(null);}} // else ignored }/* (non-Javadoc)* @see android.view.View.OnClickListener#onClick(android.view.View)*/@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.button_state1:switchState(0);break;case R.id.button_state2:switchState(1);break;case R.id.button_state3:switchState(2);break;case R.id.button_state4:switchState(3);break;default:break;}}public static interface OnTabChangeListener {public void onTabChange(String tag);} }
2、StartActivity.java 将mTabView.setCurrentTab(0); 移动到setupViews();之后
package app.ui.activity;import mobi.kuaidian.qunakao.R;import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.util.Log; import android.view.KeyEvent; import android.widget.TextView;import app.ui.FragmentCallback; import app.ui.fragment.ProfileFragment; import app.ui.fragment.ServiceFragment; import app.ui.fragment.SessionFragment; import app.ui.fragment.SettingFragment; import app.ui.widget.TabView; import app.ui.widget.TabView.OnTabChangeListener; import app.util.DialogUtils; import app.util.FragmentUtils;public class StartActivity extends FragmentActivity implements OnTabChangeListener, FragmentCallback {private FragmentManager mFragmentManager;private Fragment mCurrentFragment;private TabView mTabView;private TextView mTitleTextView;/** 上一次的状态 */private int mPreviousTabIndex = 1;/** 当前状态 */private int mCurrentTabIndex = 1;/** 再按一次退出程序*/private long exitTime = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);mFragmentManager = getSupportFragmentManager();mCurrentTabIndex = 1;mPreviousTabIndex = 1;setupViews();mTabView.setCurrentTab(0);}private void setupViews(){setContentView(R.layout.activity_start);mTitleTextView = (TextView) findViewById(R.id.text_title);mTabView = (TabView) findViewById(R.id.view_tab);mTabView.setOnTabChangeListener(this); // mTabView.setCurrentTab(mCurrentTabIndex);mCurrentFragment = new ServiceFragment();FragmentUtils.replaceFragment(mFragmentManager, R.id.layout_content,ServiceFragment.class, null, false);}/* (non-Javadoc)* @see android.support.v4.app.FragmentActivity#onActivityResult(int, int, android.content.Intent)*/@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);switch (requestCode) {/*case BaseActivity.REQUEST_OK_LOGIN:if (resultCode == RESULT_OK) {ApplicationUtils.showToast(this, R.string.text_loginsuccess);mTitleTextView.setText(R.string.text_tab_profile);final ProfileFragment profileFragment =(ProfileFragment) mFragmentManager.findFragmentByTag(ProfileFragment.class.getSimpleName());if (profileFragment != null) {Log.d(TAG, "ProfileFragment is refreshing.");profileFragment.refreshViews();} else {Log.e(TAG, "ProfileFragment is null.");}} else {// 返回原来的页面mTabView.setCurrentTab(mPreviousTabIndex);ApplicationUtils.showToast(this, R.string.toast_login_failed);}break;default:break;*/}}/* (non-Javadoc)* @see app.ui.FragmentCallback#onFragmentCallback(android.support.v4.app.Fragment, int, android.os.Bundle)*/@Overridepublic void onFragmentCallback(Fragment fragment, int id, Bundle args) {mTabView.setCurrentTab(1);}/* (non-Javadoc)* @see app.ui.widget.TabView.OnTabChangeListener#onTabChange(java.lang.String)*/@Overridepublic void onTabChange(String tag) {Log.e("tag", "onTabchange tag:" + tag);if (tag != null) {if (tag.equals("message")) {mPreviousTabIndex = mCurrentTabIndex;mCurrentTabIndex = 0;mTitleTextView.setText(R.string.text_tab_message);replaceFragment(SessionFragment.class);// 检查,如果没有登录则跳转到登录界面/* final UserConfigManager manager = UserConfigManager.getInstance();if (manager.getId() <= 0) {startActivityForResult(new Intent(this, LoginActivity.class),BaseActivity.REQUEST_OK_LOGIN);}*/}else if ("service".equals(tag)) {mPreviousTabIndex = mCurrentTabIndex;mCurrentTabIndex = 1;mTitleTextView.setText(R.string.text_tab_service);replaceFragment(ServiceFragment.class);} else if (tag.equals("personal")) {mPreviousTabIndex = mCurrentTabIndex;mCurrentTabIndex = 2;mTitleTextView.setText(R.string.text_tab_profile);replaceFragment(ProfileFragment.class);// 检查,如果没有登录则跳转到登录界面/* final UserConfigManager manager = UserConfigManager.getInstance();if (manager.getId() <= 0) {startActivityForResult(new Intent(this, LoginActivity.class),BaseActivity.REQUEST_OK_LOGIN);}*/} else if (tag.equals("settings")) {mPreviousTabIndex = mCurrentTabIndex;mCurrentTabIndex = 3;mTitleTextView.setText(R.string.text_tab_setting);replaceFragment(SettingFragment.class);// 检查,如果没有登录则跳转到登录界面/* final UserConfigManager manager = UserConfigManager.getInstance();if (manager.getId() <= 0) {startActivityForResult(new Intent(this, LoginActivity.class),BaseActivity.REQUEST_OK_LOGIN);}*/}}}private void replaceFragment(Class<? extends Fragment> newFragment) {mCurrentFragment = FragmentUtils.switchFragment(mFragmentManager,R.id.layout_content, mCurrentFragment,newFragment, null, false);}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {if ((System.currentTimeMillis() - exitTime) > 2000) {DialogUtils.showToast(this, "再按一次退出程序");exitTime = System.currentTimeMillis();} else {finish();System.exit(0);}return true;}return super.onKeyDown(keyCode, event);} }
下载地址:http://download.csdn.net/detail/zhanyujiez/8512197