1.声明

private FragmentManager fManager;//管理Fragment界面

2.初始化

fManager = getSupportFragmentManager();

3.切换代码

/**     * Fragment切换     *     * @param index     */    private void setChoiceItem(int index) {        FragmentTransaction transaction = fManager.beginTransaction();        clearChoice();        hideFragments(transaction);        switch (index) {            case 0:                img_home.setBackgroundResource(R.drawable.home_select);                if (home == null) {                    home = new HomeFragment();                    transaction.add(R.id.main_frame_layout, home);                } else {                    transaction.show(home);                }                break;            case 1:                img_message.setBackgroundResource(R.drawable.message_select);                if (message == null) {                    message = new MessageFragment();                    transaction.add(R.id.main_frame_layout, message);                } else {                    transaction.show(message);                }                break;            case 2:                img_attention.setBackgroundResource(R.drawable.attention_select);                if (attention == null) {                    attention = new AttentionFragment();                    transaction.add(R.id.main_frame_layout, attention);                } else {                    transaction.show(attention);                }                break;            case 3:                img_mine.setBackgroundResource(R.drawable.mine_select);                if (mine == null) {                    mine = new MineFragment();                    transaction.add(R.id.main_frame_layout, mine);                } else {                    transaction.show(mine);                }                break;        }        transaction.commit();    }

4.隐藏

/**     * 隐藏片段     *     * @param transaction     */    private void hideFragments(FragmentTransaction transaction) {        if (home != null) {            transaction.hide(home);        }        if (message != null) {            transaction.hide(message);        }        if (attention != null) {            transaction.hide(attention);        }        if (mine != null) {            transaction.hide(mine);        }    }

5.清除选中样式

/**     * 清除选中Fragment的样式     */    public void clearChoice() {        img_home.setBackgroundResource(R.drawable.home_unselect);        img_message.setBackgroundResource(R.drawable.message_unselect);        img_attention.setBackgroundResource(R.drawable.attention_unselect);        img_mine.setBackgroundResource(R.drawable.mine_unselect);    }

更多相关文章

  1. 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Fedora镜像
  2. 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像
  3. Android(安卓)Activity界面切换添加动画特效
  4. Android(安卓)Activity界面切换添加动画特效
  5. [android]在上下文菜单的选中事件中获取列表选中的元素
  6. Android,LIstView中的OnItemClick点击无效的解决办法
  7. android调用setRequestedOrientation之后
  8. Android(安卓)之 下拉框(Spinner)的简单使用
  9. android listview custom style 自定义样式

随机推荐

  1. Android内核的简单分析
  2. 用angularjs写android应用连手机调试的步
  3. 如何将应用发布到市场上去
  4. 安卓中的shape
  5. Android(安卓)NFS文件系统挂载遇到的问题
  6. 关于android studio开发APP中,给单个Activ
  7. Android之View篇1————初识View
  8. 关于android的nfc问题
  9. Android(安卓)SDCard Mount 流程分析
  10. Android(安卓)Sensor传感器系统架构初探