// BaseActivity 是實現的一些公共的變量和方法,能夠暫時爲空的類
public class HomeActivity extends BaseActivity implements View.OnClickListener {
// 下面的四個按鈕 private TextView mHomeView; private TextView mPondView; private TextView mMessageView; private TextView mMineView;
// 相對佈局的 layout 裏面裝着2個textView 分別放着 圖標和文字 private RelativeLayout mHomeLayout; private RelativeLayout mPondLayout; private RelativeLayout mMessageLayout; private RelativeLayout mMineLayout; // 聲明fragmentManager 和四個fragment 以及當前的fragment private FragmentManager fm; private HomeFragment mHomeFragment; private Fragment mCommonFragmentOne; private MessageFragment mMessageFragment; private MineFragment mMineFragment; private Fragment mCurrent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home_layout);
// 初始化控件變量 initView();
// 建立首頁的Fragment mHomeFragment = new HomeFragment();
// 獲得fragment 管理器,Activity 基類裏面定義的 fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
// 使用homeFragment 替換 content_layout 佈局 fragmentTransaction.replace(R.id.content_layout, mHomeFragment); fragmentTransaction.commit(); } private void initView() {
// 先初始化layout 並賦值其點擊的方法 mHomeLayout = (RelativeLayout) findViewById(R.id.home_layout_view); mHomeLayout.setOnClickListener(this); mPondLayout = (RelativeLayout) findViewById(R.id.pond_layout_view); mPondLayout.setOnClickListener(this); mMessageLayout = (RelativeLayout) findViewById(R.id.message_layout_view); mMessageLayout.setOnClickListener(this); mMineLayout = (RelativeLayout) findViewById(R.id.mine_layout_view); mMineLayout.setOnClickListener(this);
// 再初始化裏面的textView 由於點擊的時候要給textView的背景換一張圖片
// 注意這裏面用了一個空的TextView,背景是一個圖標,點擊時換的就是這個圖標 mHomeView = (TextView) findViewById(R.id.home_image_view); mPondView = (TextView) findViewById(R.id.fish_image_view); mMessageView = (TextView) findViewById(R.id.message_image_view); mMineView = (TextView) findViewById(R.id.mine_image_view); mHomeView.setBackgroundResource(R.drawable.comui_tab_home_selected); }
// 使用 fragmentTransaction 隱藏fragment private void hideFragment(Fragment fragment, FragmentTransaction ft) { if (fragment != null) { ft.hide(fragment); } } @Override public void onClick(View v) { FragmentTransaction fragmentTransaction = fm.beginTransaction(); switch (v.getId()) { case R.id.home_layout_view: // 非必須,能夠刪除掉
changeStatusBarColor(R.color.color_fed952);
// 更改背景圖片 mHomeView.setBackgroundResource(R.drawable.comui_tab_home_selected); mPondView.setBackgroundResource(R.drawable.comui_tab_pond); mMessageView.setBackgroundResource(R.drawable.comui_tab_message); mMineView.setBackgroundResource(R.drawable.comui_tab_person); // 隱藏其餘fragment hideFragment(mCommonFragmentOne, fragmentTransaction); hideFragment(mMessageFragment, fragmentTransaction); hideFragment(mMineFragment, fragmentTransaction); if (mHomeFragment == null) {
//添加一個新的fragment mHomeFragment = new HomeFragment(); fragmentTransaction.add(R.id.content_layout, mHomeFragment); } else {
// 顯示當前fragment mCurrent = mHomeFragment; fragmentTransaction.show(mHomeFragment); } break; case R.id.message_layout_view: changeStatusBarColor(R.color.color_e3e3e3); mMessageView.setBackgroundResource(R.drawable.comui_tab_message_selected); mHomeView.setBackgroundResource(R.drawable.comui_tab_home); mPondView.setBackgroundResource(R.drawable.comui_tab_pond); mMineView.setBackgroundResource(R.drawable.comui_tab_person); hideFragment(mCommonFragmentOne, fragmentTransaction); hideFragment(mHomeFragment, fragmentTransaction); hideFragment(mMineFragment, fragmentTransaction); if (mMessageFragment == null) { mMessageFragment = new MessageFragment(); fragmentTransaction.add(R.id.content_layout, mMessageFragment); } else { mCurrent = mMessageFragment; fragmentTransaction.show(mMessageFragment); } break; case R.id.mine_layout_view: changeStatusBarColor(R.color.color_00ffffff); mMineView.setBackgroundResource(R.drawable.comui_tab_person_selected); mHomeView.setBackgroundResource(R.drawable.comui_tab_home); mPondView.setBackgroundResource(R.drawable.comui_tab_pond); mMessageView.setBackgroundResource(R.drawable.comui_tab_message); hideFragment(mCommonFragmentOne, fragmentTransaction); hideFragment(mMessageFragment, fragmentTransaction); hideFragment(mHomeFragment, fragmentTransaction); if (mMineFragment == null) { mMineFragment = new MineFragment(); fragmentTransaction.add(R.id.content_layout, mMineFragment); } else { mCurrent = mMineFragment; fragmentTransaction.show(mMineFragment); } break; } fragmentTransaction.commit(); } }