大家晚上好,我是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)。。。
看下效果图吧。。。其实也很简单。。。。建议你认真看源码。。。。。
这里放下主要源码:
复制代码
HomeFrameFragment 所加载的xml文件:
复制代码
include的xml文件:actionbar_home.xml
复制代码
include的xml文件:
item_menu_home_tab
复制代码
四个子Fragment的其中一个:HomeFragment
复制代码
有一点要说下,程序中所出现的汉字,最好放在value文件下的string.xml里,当然,我为了省时间,直接写上了,千万不要有这个坏习惯。除法你的应用只做国内的。。。呵呵。。。。
自嘲一下,真不知道我四级是怎么过的,取的类名都那么没水平,不过也有好处的,就是容易看懂,希望网友认真的看,,,,不懂的多多问,我懂一定答。。。呵呵。。。。明天不用上班,也不用回学校上课。。。。睡大觉。。。。。哈哈哈。。。。。。
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)。。。
看下效果图吧。。。其实也很简单。。。。建议你认真看源码。。。。。
这里放下主要源码:
- </blockquote></div><div class="blockcode"><blockquote>package com.cjj.shopapp.fragment;
-
- import com.cjj.shopapp.activity.MainActivity;
- import com.cjj.shopapp.activity.R;
-
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentManager;
- import android.support.v4.app.FragmentTransaction;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup;
- import android.widget.CompoundButton;
- import android.widget.ImageButton;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.RadioGroup.OnCheckedChangeListener;
-
- public class HomeFrameFragment extends Fragment implements OnClickListener,OnCheckedChangeListener{
- /**显示slidming按钮*/
- private ImageButton ibtn_left_menu;
- /**四个子Fragment*/
- private HomeFragment homeFragment;
- private CategoryFragment categoryFragment;
- private HotFragment hotFragment;
- private AboutFragment aboutFragment;
- /**四个子Fragment的Tag*/
- public static final String TAG_HOME = "Home";
- public static final String TAG_CATEGORY = "Category";
- public static final String TAG_HOT = "hot";
- public static final String TAG_ABOUT = "About";
-
- /**显示RG按钮*/
- private RadioGroup rg_home_tab_menu;
-
- private FragmentManager mFragmentManager;
- private FragmentTransaction mFragmentTransaction;
- private String hideTag;
-
-
-
- @Override
- public void onActivityCreated(Bundle savedInstanceState) {
- super.onActivityCreated(savedInstanceState);
- }
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
-
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View v = inflater.inflate(R.layout.fragment_home_frame, null);
- return v;
- }
-
-
- @Override
- public void onDestroyView() {
- super.onDestroyView();
- }
-
- @Override
- public void onViewCreated(View view, Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- ibtn_left_menu = (ImageButton) view.findViewById(R.id.ibtn_left_menu);
- ibtn_left_menu.setOnClickListener(this);
-
- rg_home_tab_menu = (RadioGroup) view.findViewById(R.id.rg_tab_menu);
- //设置第一个radiobutton为选中状态
- RadioButton rbtn = (RadioButton) rg_home_tab_menu.getChildAt(0);
- rbtn.setChecked(true);
- //设置radiogroud监听
- rg_home_tab_menu.setOnCheckedChangeListener(this);
-
- homeFragment = new HomeFragment();
- switchFragment(homeFragment,TAG_HOME);
- }
- /**
- * 选择不同的fragment
- */
- private void switchFragment(Fragment fragment,String tag) {
- mFragmentManager = getChildFragmentManager();
- mFragmentTransaction = mFragmentManager.beginTransaction();
-
- Fragment tagFragment = mFragmentManager.findFragmentByTag(tag);
-
- if(tagFragment == null){
- mFragmentTransaction.add(R.id.fl_tab_menu, fragment, tag);
- }else{
- mFragmentTransaction.show(tagFragment);
- }
-
- tagFragment = mFragmentManager.findFragmentByTag(hideTag);
-
- if(tagFragment!=null){
- mFragmentTransaction.hide(tagFragment);
- }
-
- hideTag = tag;
- mFragmentTransaction.commit();
- }
-
- @Override
- public void onClick(View v) {
- switch(v.getId()){
- case R.id.ibtn_left_menu:
- ((MainActivity) getActivity()).showMenu();
- break;
- }
-
- }
-
- @Override
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- switch(checkedId){
- case R.id.rbtn_one:
- if(homeFragment == null){
- homeFragment = new HomeFragment();
- }
- switchFragment(homeFragment, TAG_HOME);
- break;
- case R.id.rbtn_two:
- if(categoryFragment == null){
- categoryFragment = new CategoryFragment();
- }
- switchFragment(categoryFragment, TAG_CATEGORY);
- break;
- case R.id.rbtn_three:
- if(hotFragment == null){
- hotFragment = new HotFragment();
- }
- switchFragment(hotFragment, TAG_HOT);
- break;
- case R.id.rbtn_four:
- if(aboutFragment == null){
- aboutFragment = new AboutFragment();
- }
- switchFragment(aboutFragment, TAG_ABOUT);
- break;
- }
- }
-
-
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
-
- <include layout="@layout/actionbar_home" />
-
- <include layout="@layout/item_menu_home_tab" />
-
- <FrameLayout
- android:id="@+id/fl_tab_menu"
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="1" />
-
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- style="@style/style_match_wrap"
- android:background="@color/app_default_bg_f5"
- android:orientation="vertical" >
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="45dp"
- android:orientation="horizontal" >
-
- <ImageButton
- android:id="@+id/ibtn_left_menu"
- android:layout_width="48dp"
- android:layout_height="@dimen/actionbar_height"
- android:background="@android:color/transparent"
- android:src="@drawable/selector_side_menu" />
-
- <View
- android:layout_width="2dp"
- android:layout_height="match_parent"
- android:layout_marginBottom="5dp"
- android:layout_marginTop="5dip"
- android:background="@drawable/line_actionbar_divider" />
-
- <TextView
- android:text="CJJ漫画"
- android:id="@+id/tv_title"
- android:layout_width="match_parent"
- android:layout_height="48dp"
- android:layout_marginLeft="12dp"
- android:layout_weight="1"
- android:gravity="center_vertical|left"
- android:paddingTop="2dp"
- android:singleLine="true"
- android:textColor="@color/cs_51"
- android:textSize="18sp" />
-
-
- </LinearLayout>
-
- <View
- android:layout_width="match_parent"
- android:layout_height="1dp"
- android:background="@drawable/bottom_bar_shadow" />
-
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="1dp"
- android:orientation="vertical" >
-
- <RadioGroup
- android:id="@+id/rg_tab_menu"
- android:layout_width="match_parent"
- android:layout_height="35dp"
- android:background="@color/white"
- android:orientation="horizontal" >
-
- <RadioButton
- android:id="@+id/rbtn_one"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:background="@drawable/selector_rbtn_home_tab_menu"
- android:button="@null"
- android:gravity="center"
- android:text="首页"
- android:checked="true"
- android:textColor="@color/selector_rbtn_menu_txt"
- android:textSize="16sp" />
-
- <View
- android:layout_width="1dp"
- android:layout_height="match_parent"
- android:layout_marginBottom="10dp"
- android:layout_marginTop="10dp"
- android:background="#e5e5e5" />
-
- <RadioButton
- android:id="@+id/rbtn_two"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:background="@drawable/selector_rbtn_home_tab_menu"
- android:button="@null"
- android:gravity="center"
- android:text="分类"
- android:textColor="@color/selector_rbtn_menu_txt"
- android:textSize="16sp" />
-
- <View
- android:layout_width="1dp"
- android:layout_height="match_parent"
- android:layout_marginBottom="10dp"
- android:layout_marginTop="10dp"
- android:background="#e5e5e5" />
-
- <RadioButton
- android:id="@+id/rbtn_three"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:background="@drawable/selector_rbtn_home_tab_menu"
- android:button="@null"
- android:gravity="center"
- android:text="热门"
- android:textColor="@color/selector_rbtn_menu_txt"
- android:textSize="16sp" />
-
- <View
- android:layout_width="1dp"
- android:layout_height="match_parent"
- android:layout_marginBottom="10dp"
- android:layout_marginTop="10dp"
- android:background="#e5e5e5" />
-
- <RadioButton
- android:id="@+id/rbtn_four"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:background="@drawable/selector_rbtn_home_tab_menu"
- android:button="@null"
- android:gravity="center"
- android:text="关于"
- android:textColor="@color/selector_rbtn_menu_txt"
- android:textSize="16sp" />
- </RadioGroup>
-
- <View
- android:layout_width="match_parent"
- android:layout_height="1dp"
- android:background="#9F9F9F" />
-
- <View
- android:layout_width="match_parent"
- android:layout_height="2dp"
- android:background="#B7B8B8" />
-
- </LinearLayout>
- package com.cjj.shopapp.fragment;
-
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.TextView;
-
- public class HomeFragment extends Fragment{
- @Override
- public void onActivityCreated(Bundle savedInstanceState) {
- super.onActivityCreated(savedInstanceState);
- }
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- }
-
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- TextView tv = new TextView(getActivity());
- tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
- tv.setGravity(Gravity.CENTER);
- tv.setTextSize(20);
- tv.setText("home");
- return tv;
- }
-
- @Override
- public void onDestroyView() {
- super.onDestroyView();
- }
-
- @Override
- public void onViewCreated(View view, Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- }
- }
自嘲一下,真不知道我四级是怎么过的,取的类名都那么没水平,不过也有好处的,就是容易看懂,希望网友认真的看,,,,不懂的多多问,我懂一定答。。。呵呵。。。。明天不用上班,也不用回学校上课。。。。睡大觉。。。。。哈哈哈。。。。。。