码迷,mamicode.com
首页 > 移动开发 > 详细

Android 一个Activity 里面放置多个 Fragment 实现点击切换的Tab 页面效果

时间:2017-07-23 09:58:27      阅读:351      评论:0      收藏:0      [点我收藏+]

标签:source   oncreate   mcu   usb   manager   blog   cte   sage   布局   


 // 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(); } }

 

Android 一个Activity 里面放置多个 Fragment 实现点击切换的Tab 页面效果

标签:source   oncreate   mcu   usb   manager   blog   cte   sage   布局   

原文地址:http://www.cnblogs.com/oftenlin/p/7223484.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!