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

大家晚上好,我是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.     
  31.     /**显示RG按钮*/
  32.     private RadioGroup rg_home_tab_menu;
  33.     
  34.     private FragmentManager mFragmentManager;
  35.     private FragmentTransaction mFragmentTransaction;
  36.     private String hideTag;
  37.         
  38.         
  39.         
  40.         @Override
  41.         public void onActivityCreated(Bundle savedInstanceState) {
  42.                 super.onActivityCreated(savedInstanceState);
  43.         }

  44.         @Override
  45.         public void onCreate(Bundle savedInstanceState) {
  46.                 super.onCreate(savedInstanceState);
  47.         }

  48.         @Override
  49.         public View onCreateView(LayoutInflater inflater, ViewGroup container,
  50.                         Bundle savedInstanceState) {
  51.                 View v = inflater.inflate(R.layout.fragment_home_frame, null);
  52.                 return v;
  53.         }

  54.         
  55.         @Override
  56.         public void onDestroyView() {
  57.                 super.onDestroyView();
  58.         }

  59.         @Override
  60.         public void onViewCreated(View view, Bundle savedInstanceState) {
  61.                 super.onViewCreated(view, savedInstanceState);
  62.                 ibtn_left_menu = (ImageButton) view.findViewById(R.id.ibtn_left_menu);
  63.                 ibtn_left_menu.setOnClickListener(this);
  64.                 
  65.                 rg_home_tab_menu = (RadioGroup) view.findViewById(R.id.rg_tab_menu);
  66.                 //设置第一个radiobutton为选中状态
  67.                 RadioButton rbtn = (RadioButton) rg_home_tab_menu.getChildAt(0);
  68.                 rbtn.setChecked(true);
  69.                 //设置radiogroud监听
  70.                 rg_home_tab_menu.setOnCheckedChangeListener(this);
  71.                 
  72.                 homeFragment = new HomeFragment();
  73.                 switchFragment(homeFragment,TAG_HOME);
  74.         }
  75.         /**
  76.          * 选择不同的fragment
  77.          */
  78.         private void switchFragment(Fragment fragment,String tag) {
  79.                 mFragmentManager = getChildFragmentManager();
  80.                 mFragmentTransaction = mFragmentManager.beginTransaction();
  81.                 
  82.                 Fragment tagFragment = mFragmentManager.findFragmentByTag(tag);
  83.                 
  84.                 if(tagFragment == null){
  85.                         mFragmentTransaction.add(R.id.fl_tab_menu, fragment, tag);
  86.                 }else{
  87.                         mFragmentTransaction.show(tagFragment);
  88.                 }
  89.                 
  90.                 tagFragment = mFragmentManager.findFragmentByTag(hideTag);
  91.                 
  92.                 if(tagFragment!=null){
  93.                         mFragmentTransaction.hide(tagFragment);
  94.                 }
  95.                 
  96.                 hideTag = tag;
  97.                 mFragmentTransaction.commit();
  98.         }

  99.         @Override
  100.         public void onClick(View v) {
  101.                 switch(v.getId()){
  102.                 case R.id.ibtn_left_menu:
  103.                         ((MainActivity) getActivity()).showMenu();
  104.                         break;
  105.                 }
  106.                 
  107.         }

  108.         @Override
  109.         public void onCheckedChanged(RadioGroup group, int checkedId) {
  110.                 switch(checkedId){
  111.                 case R.id.rbtn_one:
  112.                         if(homeFragment == null){
  113.                                 homeFragment = new HomeFragment();
  114.                         }
  115.                         switchFragment(homeFragment, TAG_HOME);
  116.                         break;
  117.                 case R.id.rbtn_two:
  118.                         if(categoryFragment == null){
  119.                                 categoryFragment = new CategoryFragment();
  120.                         }
  121.                         switchFragment(categoryFragment, TAG_CATEGORY);
  122.                         break;
  123.                 case R.id.rbtn_three:
  124.                         if(hotFragment == null){
  125.                                 hotFragment = new HotFragment();
  126.                         }
  127.                         switchFragment(hotFragment, TAG_HOT);
  128.                         break;
  129.                 case R.id.rbtn_four:
  130.                         if(aboutFragment == null){
  131.                                 aboutFragment = new AboutFragment();
  132.                         }
  133.                         switchFragment(aboutFragment, TAG_ABOUT);
  134.                         break;
  135.                 }
  136.         }


  137. }
复制代码
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.        
  35.     </LinearLayout>

  36.     <View
  37.         android:layout_width="match_parent"
  38.         android:layout_height="1dp"
  39.         android:background="@drawable/bottom_bar_shadow" />

  40. </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里,当然,我为了省时间,直接写上了,千万不要有这个坏习惯。除法你的应用只做国内的。。。呵呵。。。。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值