大家晚上好,我是CJJ,继昨天写好框架之后,今天上班一直在想做什么东西。。。本来想拿我即将要上交的毕业设计做教程的,但是想想好像在重复工作那样子。。。。呵呵 。。。 伟大的先人说过,不要重复制造轮子。。。。因本人很喜欢漫画,所以决定做一个漫画APP。。。当然。。。。我尽量不要做的太烂就可以了。。。。大神勿喷。。。初学者一起学习吧。。。。呵呵 android真实项目教程(一)——App应用框架搭建_by_CJJ http://www.apkbus.com/forum.php?mod=viewthread&tid=166151
android真实项目教程(二)——漫画App初构_by_CJJ http://www.apkbus.com/forum.php?mod=viewthread&tid=166262
android真实项目教程(三)——首页初点缀_by_CJJ http://www.apkbus.com/forum.php?mod=viewthread&tid=166630
android真实项目教程(四)——MY APP MY STYLE_by_CJJ http://www.apkbus.com/forum.php?mod=viewthread&tid=167676
android真实项目教程(五)——有时三点两点雨_by_CJJ http://www.apkbus.com/forum.php?mod=viewthread&tid=168422

上文说到MainActivity包含着一个Fragment——HomeFrameFragment(主Fragment),我想在HomeFragment嵌入子Fragment。。。。做一个tab导航栏,不断的跟换四个子Fragment(HomeFragment,CategoryFragment,HotFragment,AboutFrament)。。。
看下效果图吧。。。其实也很简单。。。。建议你认真看源码。。。。。

这里放下主要源码:

  1. </blockquote></div><div class="blockcode"><blockquote>package com.cjj.shopapp.fragment;
  2. import com.cjj.shopapp.activity.MainActivity;
  3. import com.cjj.shopapp.activity.R;
  4. import android.os.Bundle;
  5. import android.support.v4.app.Fragment;
  6. import android.support.v4.app.FragmentManager;
  7. import android.support.v4.app.FragmentTransaction;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.view.ViewGroup;
  12. import android.widget.CompoundButton;
  13. import android.widget.ImageButton;
  14. import android.widget.RadioButton;
  15. import android.widget.RadioGroup;
  16. import android.widget.RadioGroup.OnCheckedChangeListener;
  17. public class HomeFrameFragment extends Fragment implements OnClickListener,OnCheckedChangeListener{
  18. /**显示slidming按钮*/
  19. private ImageButton ibtn_left_menu;
  20. /**四个子Fragment*/
  21. private HomeFragment homeFragment;
  22. private CategoryFragment categoryFragment;
  23. private HotFragment hotFragment;
  24. private AboutFragment aboutFragment;
  25. /**四个子Fragment的Tag*/
  26. public static final String TAG_HOME = "Home";
  27. public static final String TAG_CATEGORY = "Category";
  28. public static final String TAG_HOT = "hot";
  29. public static final String TAG_ABOUT = "About";
  30. /**显示RG按钮*/
  31. private RadioGroup rg_home_tab_menu;
  32. private FragmentManager mFragmentManager;
  33. private FragmentTransaction mFragmentTransaction;
  34. private String hideTag;
  35. @Override
  36. public void onActivityCreated(Bundle savedInstanceState) {
  37. super.onActivityCreated(savedInstanceState);
  38. }
  39. @Override
  40. public void onCreate(Bundle savedInstanceState) {
  41. super.onCreate(savedInstanceState);
  42. }
  43. @Override
  44. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  45. Bundle savedInstanceState) {
  46. View v = inflater.inflate(R.layout.fragment_home_frame, null);
  47. return v;
  48. }
  49. @Override
  50. public void onDestroyView() {
  51. super.onDestroyView();
  52. }
  53. @Override
  54. public void onViewCreated(View view, Bundle savedInstanceState) {
  55. super.onViewCreated(view, savedInstanceState);
  56. ibtn_left_menu = (ImageButton) view.findViewById(R.id.ibtn_left_menu);
  57. ibtn_left_menu.setOnClickListener(this);
  58. rg_home_tab_menu = (RadioGroup) view.findViewById(R.id.rg_tab_menu);
  59. //设置第一个radiobutton为选中状态
  60. RadioButton rbtn = (RadioButton) rg_home_tab_menu.getChildAt(0);
  61. rbtn.setChecked(true);
  62. //设置radiogroud监听
  63. rg_home_tab_menu.setOnCheckedChangeListener(this);
  64. homeFragment = new HomeFragment();
  65. switchFragment(homeFragment,TAG_HOME);
  66. }
  67. /**
  68. * 选择不同的fragment
  69. */
  70. private void switchFragment(Fragment fragment,String tag) {
  71. mFragmentManager = getChildFragmentManager();
  72. mFragmentTransaction = mFragmentManager.beginTransaction();
  73. Fragment tagFragment = mFragmentManager.findFragmentByTag(tag);
  74. if(tagFragment == null){
  75. mFragmentTransaction.add(R.id.fl_tab_menu, fragment, tag);
  76. }else{
  77. mFragmentTransaction.show(tagFragment);
  78. }
  79. tagFragment = mFragmentManager.findFragmentByTag(hideTag);
  80. if(tagFragment!=null){
  81. mFragmentTransaction.hide(tagFragment);
  82. }
  83. hideTag = tag;
  84. mFragmentTransaction.commit();
  85. }
  86. @Override
  87. public void onClick(View v) {
  88. switch(v.getId()){
  89. case R.id.ibtn_left_menu:
  90. ((MainActivity) getActivity()).showMenu();
  91. break;
  92. }
  93. }
  94. @Override
  95. public void onCheckedChanged(RadioGroup group, int checkedId) {
  96. switch(checkedId){
  97. case R.id.rbtn_one:
  98. if(homeFragment == null){
  99. homeFragment = new HomeFragment();
  100. }
  101. switchFragment(homeFragment, TAG_HOME);
  102. break;
  103. case R.id.rbtn_two:
  104. if(categoryFragment == null){
  105. categoryFragment = new CategoryFragment();
  106. }
  107. switchFragment(categoryFragment, TAG_CATEGORY);
  108. break;
  109. case R.id.rbtn_three:
  110. if(hotFragment == null){
  111. hotFragment = new HotFragment();
  112. }
  113. switchFragment(hotFragment, TAG_HOT);
  114. break;
  115. case R.id.rbtn_four:
  116. if(aboutFragment == null){
  117. aboutFragment = new AboutFragment();
  118. }
  119. switchFragment(aboutFragment, TAG_ABOUT);
  120. break;
  121. }
  122. }
  123. }

复制代码

HomeFrameFragment 所加载的xml文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <include layout="@layout/actionbar_home" />
  7. <include layout="@layout/item_menu_home_tab" />
  8. <FrameLayout
  9. android:id="@+id/fl_tab_menu"
  10. android:layout_width="match_parent"
  11. android:layout_height="0dp"
  12. android:layout_weight="1" />
  13. </LinearLayout>

复制代码

include的xml文件:actionbar_home.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. style="@style/style_match_wrap"
  4. android:background="@color/app_default_bg_f5"
  5. android:orientation="vertical" >
  6. <LinearLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="45dp"
  9. android:orientation="horizontal" >
  10. <ImageButton
  11. android:id="@+id/ibtn_left_menu"
  12. android:layout_width="48dp"
  13. android:layout_height="@dimen/actionbar_height"
  14. android:background="@android:color/transparent"
  15. android:src="@drawable/selector_side_menu" />
  16. <View
  17. android:layout_width="2dp"
  18. android:layout_height="match_parent"
  19. android:layout_marginBottom="5dp"
  20. android:layout_marginTop="5dip"
  21. android:background="@drawable/line_actionbar_divider" />
  22. <TextView
  23. android:text="CJJ漫画"
  24. android:id="@+id/tv_title"
  25. android:layout_width="match_parent"
  26. android:layout_height="48dp"
  27. android:layout_marginLeft="12dp"
  28. android:layout_weight="1"
  29. android:gravity="center_vertical|left"
  30. android:paddingTop="2dp"
  31. android:singleLine="true"
  32. android:textColor="@color/cs_51"
  33. android:textSize="18sp" />
  34. </LinearLayout>
  35. <View
  36. android:layout_width="match_parent"
  37. android:layout_height="1dp"
  38. android:background="@drawable/bottom_bar_shadow" />
  39. </LinearLayout>

复制代码

include的xml文件: item_menu_home_tab

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:layout_marginTop="1dp"
  6. android:orientation="vertical" >
  7. <RadioGroup
  8. android:id="@+id/rg_tab_menu"
  9. android:layout_width="match_parent"
  10. android:layout_height="35dp"
  11. android:background="@color/white"
  12. android:orientation="horizontal" >
  13. <RadioButton
  14. android:id="@+id/rbtn_one"
  15. android:layout_width="match_parent"
  16. android:layout_height="match_parent"
  17. android:layout_weight="1"
  18. android:background="@drawable/selector_rbtn_home_tab_menu"
  19. android:button="@null"
  20. android:gravity="center"
  21. android:text="首页"
  22. android:checked="true"
  23. android:textColor="@color/selector_rbtn_menu_txt"
  24. android:textSize="16sp" />
  25. <View
  26. android:layout_width="1dp"
  27. android:layout_height="match_parent"
  28. android:layout_marginBottom="10dp"
  29. android:layout_marginTop="10dp"
  30. android:background="#e5e5e5" />
  31. <RadioButton
  32. android:id="@+id/rbtn_two"
  33. android:layout_width="match_parent"
  34. android:layout_height="match_parent"
  35. android:layout_weight="1"
  36. android:background="@drawable/selector_rbtn_home_tab_menu"
  37. android:button="@null"
  38. android:gravity="center"
  39. android:text="分类"
  40. android:textColor="@color/selector_rbtn_menu_txt"
  41. android:textSize="16sp" />
  42. <View
  43. android:layout_width="1dp"
  44. android:layout_height="match_parent"
  45. android:layout_marginBottom="10dp"
  46. android:layout_marginTop="10dp"
  47. android:background="#e5e5e5" />
  48. <RadioButton
  49. android:id="@+id/rbtn_three"
  50. android:layout_width="match_parent"
  51. android:layout_height="match_parent"
  52. android:layout_weight="1"
  53. android:background="@drawable/selector_rbtn_home_tab_menu"
  54. android:button="@null"
  55. android:gravity="center"
  56. android:text="热门"
  57. android:textColor="@color/selector_rbtn_menu_txt"
  58. android:textSize="16sp" />
  59. <View
  60. android:layout_width="1dp"
  61. android:layout_height="match_parent"
  62. android:layout_marginBottom="10dp"
  63. android:layout_marginTop="10dp"
  64. android:background="#e5e5e5" />
  65. <RadioButton
  66. android:id="@+id/rbtn_four"
  67. android:layout_width="match_parent"
  68. android:layout_height="match_parent"
  69. android:layout_weight="1"
  70. android:background="@drawable/selector_rbtn_home_tab_menu"
  71. android:button="@null"
  72. android:gravity="center"
  73. android:text="关于"
  74. android:textColor="@color/selector_rbtn_menu_txt"
  75. android:textSize="16sp" />
  76. </RadioGroup>
  77. <View
  78. android:layout_width="match_parent"
  79. android:layout_height="1dp"
  80. android:background="#9F9F9F" />
  81. <View
  82. android:layout_width="match_parent"
  83. android:layout_height="2dp"
  84. android:background="#B7B8B8" />
  85. </LinearLayout>

复制代码

四个子Fragment的其中一个:HomeFragment

  1. package com.cjj.shopapp.fragment;
  2. import android.os.Bundle;
  3. import android.support.v4.app.Fragment;
  4. import android.view.Gravity;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.view.ViewGroup.LayoutParams;
  9. import android.widget.TextView;
  10. public class HomeFragment extends Fragment{
  11. @Override
  12. public void onActivityCreated(Bundle savedInstanceState) {
  13. super.onActivityCreated(savedInstanceState);
  14. }
  15. @Override
  16. public void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. }
  19. @Override
  20. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  21. Bundle savedInstanceState) {
  22. TextView tv = new TextView(getActivity());
  23. tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
  24. tv.setGravity(Gravity.CENTER);
  25. tv.setTextSize(20);
  26. tv.setText("home");
  27. return tv;
  28. }
  29. @Override
  30. public void onDestroyView() {
  31. super.onDestroyView();
  32. }
  33. @Override
  34. public void onViewCreated(View view, Bundle savedInstanceState) {
  35. super.onViewCreated(view, savedInstanceState);
  36. }
  37. }

复制代码

有一点要说下,程序中所出现的汉字,最好放在value文件下的string.xml里,当然,我为了省时间,直接写上了,千万不要有这个坏习惯。除法你的应用只做国内的。。。呵呵。。。。

自嘲一下,真不知道我四级是怎么过的,取的类名都那么没水平,不过也有好处的,就是容易看懂,希望网友认真的看,,,,不懂的多多问,我懂一定答。。。呵呵。。。。明天不用上班,也不用回学校上课。。。。睡大觉。。。。。哈哈哈。。。。。。

android真实项目教程(二)——漫画App初构_by_CJJ相关推荐

  1. android真实项目教程(六)——落叶醉赤壁_by_CJJ

    大家晚上好,我是cjj,今天不讲废话,因为我被"忙"了... 今晚主要把关于的界面(aboutFragment)完成了...效果很好哦 (自吹一下)...呵呵...其中有两个比较好 ...

  2. android真实项目教程(四)——MY APP MY STYLE_by_CJJ

    大家下午好...如果在学校,到时间吃晚饭了....隔了好久才重新敲代码...又落后那么多了,要更加努力学习了....今天下午写了下app的第四部分... 这里给下之前三部分的地址,因为如果第一次看,, ...

  3. android真实项目教程(三)——首页初点缀_by_CJJ

    大家晚上好,CJJ不好,前天打球,把右手弄脱臼了...搞得我现在只能一只手敲代码...那效率,我给自己跪了 ...写了好久,才写了那么一丁点...明明还有好多要说的...也只能等手好了再继续吧...呵 ...

  4. android真实项目教程(一)——App应用框架搭建_by_CJJ

    大家好,我是CJJ,学android半年了,仍然是菜虫一只......为了进步,想把自己知道的知识和初学者分享,也希望路过的大神能给些意见....呵呵......开始今天的教程吧,晕,不敢说教程了 , ...

  5. android真实项目教程(七)——梦醒边缘花落_by_CJJ

    大家下午好,我是CJJ,说说昨晚挑灯夜写毕业论文到凌晨三点多,当写到致谢词那块时,我违心的写下: " 本论文在xxx导师的悉 心指导和亲切关怀下完成的.导师渊博的专业知识.严谨的治学态度,精 ...

  6. android真实项目教程(五)——有时三点两点雨_by_CJJ

    大家傍晚好,我是cjj,过多十几分钟就下班了,肚子饿到要死,马上可以吃饭了...呵呵...最近事情很多,要做毕设写论文,又要上班,班里又搞什么班照,系里又一些鸡毛小事就可以累死你...晕...好像在传 ...

  7. Android WebRTC 入门教程(二) -- 模拟p2p本地视频传输

    Android WebRTC 入门教程(一) – 使用相机 Android WebRTC 入门教程(二) – 模拟p2p本地视频传输 源码工程: https://github.com/LillteZh ...

  8. GitHub上不错的Android开源项目(二)

    摘要:GitHub上的开源项目不胜枚举,通过这些项目,也能让开发者在应用开发过程中事半功倍,作为开发者的你,在用这些开源项目吗?今天我们将介绍另外20个在GitHub上备受欢迎的Android开源项目 ...

  9. GitHub上最火的40个Android开源项目(二)

    在<GitHub上最火的40个Android开源项目(一)>中,我们详细地介绍了GitHub上最受欢迎的TOP20 Android开源项目,引起了许多读者的热议,作为开发者,你最常用的是哪 ...

最新文章

  1. C++ 获得指定路径文件的“修改日期”
  2. 第十六届智能车竞赛广东线上比赛 - 哈尔滨工业大学(深圳)比赛筹备
  3. 递归算法之排列组合-求一个集合S的m个元素的组合和所有可能的组合情况
  4. 套接字Select I/O模型
  5. MyBatis框架 注解
  6. 下载丨53页PDF,云和恩墨技术通讯(2021年4月刊)
  7. solr之搭建企业搜索平台,配置文件详细solrconfig.xml
  8. 安卓mysql修改_手动修改Android数据库数据
  9. GDAL源码剖析(八)之编译GEOS和PROJ4库
  10. 输入指定答案提示正确C语言,大学C语言课件及复习答案输入输出.ppt
  11. WebFrom 【文件上传】
  12. 前端基础知识复习之html
  13. 实训日记(一)——剧本
  14. 460. LFU缓存
  15. 1NF,2NF,3NF,BCNF范式(学习笔记)
  16. c语言运行excel中vba程序,VBA代码在WPS上可运行,在EXCEL中报错
  17. 从数据类型 varchar 转换为 numeric 时出错
  18. 自学系列 | 就谈兴趣!
  19. 如何有效实现软件的需求管理 - 5
  20. 网页头部的声明 lang=zh和 lang=zh-cn 及 lang=zh-cmn的区别

热门文章

  1. Quick Switch Virtual Desktop[AutoHotkey]
  2. 使用CompletionService结合ExecutorService批处理任务
  3. selenium+java:获取列表中的值
  4. Linux 配置LNMP服务器 并配置虚拟主机
  5. Ubuntu自定义服务
  6. cisco 密码重置
  7. EasyUI的-表格设置
  8. React-Router4按需加载
  9. 摆脱设备操作桎梏 汉锐股份教育录播系统解密
  10. mysql数据库配置优化(占cpu过高问题)