Android(安卓)界面(Fragment)切换
16lz
2022-07-27
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); }
更多相关文章
- 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Fedora镜像
- 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像
- Android(安卓)Activity界面切换添加动画特效
- Android(安卓)Activity界面切换添加动画特效
- [android]在上下文菜单的选中事件中获取列表选中的元素
- Android,LIstView中的OnItemClick点击无效的解决办法
- android调用setRequestedOrientation之后
- Android(安卓)之 下拉框(Spinner)的简单使用
- android listview custom style 自定义样式