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)。。。
看下效果图吧。。。其实也很简单。。。。建议你认真看源码。。。。。
这里放下主要源码:
- </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;
- }
- }
- }
复制代码
HomeFrameFragment 所加载的xml文件:
- <?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>
复制代码
include的xml文件:actionbar_home.xml
- <?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>
复制代码
include的xml文件: item_menu_home_tab
- <?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>
复制代码
四个子Fragment的其中一个:HomeFragment
- 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);
- }
- }
复制代码
有一点要说下,程序中所出现的汉字,最好放在value文件下的string.xml里,当然,我为了省时间,直接写上了,千万不要有这个坏习惯。除法你的应用只做国内的。。。呵呵。。。。
自嘲一下,真不知道我四级是怎么过的,取的类名都那么没水平,不过也有好处的,就是容易看懂,希望网友认真的看,,,,不懂的多多问,我懂一定答。。。呵呵。。。。明天不用上班,也不用回学校上课。。。。睡大觉。。。。。哈哈哈。。。。。。
android真实项目教程(二)——漫画App初构_by_CJJ相关推荐
- android真实项目教程(六)——落叶醉赤壁_by_CJJ
大家晚上好,我是cjj,今天不讲废话,因为我被"忙"了... 今晚主要把关于的界面(aboutFragment)完成了...效果很好哦 (自吹一下)...呵呵...其中有两个比较好 ...
- android真实项目教程(四)——MY APP MY STYLE_by_CJJ
大家下午好...如果在学校,到时间吃晚饭了....隔了好久才重新敲代码...又落后那么多了,要更加努力学习了....今天下午写了下app的第四部分... 这里给下之前三部分的地址,因为如果第一次看,, ...
- android真实项目教程(三)——首页初点缀_by_CJJ
大家晚上好,CJJ不好,前天打球,把右手弄脱臼了...搞得我现在只能一只手敲代码...那效率,我给自己跪了 ...写了好久,才写了那么一丁点...明明还有好多要说的...也只能等手好了再继续吧...呵 ...
- android真实项目教程(一)——App应用框架搭建_by_CJJ
大家好,我是CJJ,学android半年了,仍然是菜虫一只......为了进步,想把自己知道的知识和初学者分享,也希望路过的大神能给些意见....呵呵......开始今天的教程吧,晕,不敢说教程了 , ...
- android真实项目教程(七)——梦醒边缘花落_by_CJJ
大家下午好,我是CJJ,说说昨晚挑灯夜写毕业论文到凌晨三点多,当写到致谢词那块时,我违心的写下: " 本论文在xxx导师的悉 心指导和亲切关怀下完成的.导师渊博的专业知识.严谨的治学态度,精 ...
- android真实项目教程(五)——有时三点两点雨_by_CJJ
大家傍晚好,我是cjj,过多十几分钟就下班了,肚子饿到要死,马上可以吃饭了...呵呵...最近事情很多,要做毕设写论文,又要上班,班里又搞什么班照,系里又一些鸡毛小事就可以累死你...晕...好像在传 ...
- Android WebRTC 入门教程(二) -- 模拟p2p本地视频传输
Android WebRTC 入门教程(一) – 使用相机 Android WebRTC 入门教程(二) – 模拟p2p本地视频传输 源码工程: https://github.com/LillteZh ...
- GitHub上不错的Android开源项目(二)
摘要:GitHub上的开源项目不胜枚举,通过这些项目,也能让开发者在应用开发过程中事半功倍,作为开发者的你,在用这些开源项目吗?今天我们将介绍另外20个在GitHub上备受欢迎的Android开源项目 ...
- GitHub上最火的40个Android开源项目(二)
在<GitHub上最火的40个Android开源项目(一)>中,我们详细地介绍了GitHub上最受欢迎的TOP20 Android开源项目,引起了许多读者的热议,作为开发者,你最常用的是哪 ...
最新文章
- C++ 获得指定路径文件的“修改日期”
- 第十六届智能车竞赛广东线上比赛 - 哈尔滨工业大学(深圳)比赛筹备
- 递归算法之排列组合-求一个集合S的m个元素的组合和所有可能的组合情况
- 套接字Select I/O模型
- MyBatis框架 注解
- 下载丨53页PDF,云和恩墨技术通讯(2021年4月刊)
- solr之搭建企业搜索平台,配置文件详细solrconfig.xml
- 安卓mysql修改_手动修改Android数据库数据
- GDAL源码剖析(八)之编译GEOS和PROJ4库
- 输入指定答案提示正确C语言,大学C语言课件及复习答案输入输出.ppt
- WebFrom 【文件上传】
- 前端基础知识复习之html
- 实训日记(一)——剧本
- 460. LFU缓存
- 1NF,2NF,3NF,BCNF范式(学习笔记)
- c语言运行excel中vba程序,VBA代码在WPS上可运行,在EXCEL中报错
- 从数据类型 varchar 转换为 numeric 时出错
- 自学系列 | 就谈兴趣!
- 如何有效实现软件的需求管理 - 5
- 网页头部的声明 lang=zh和 lang=zh-cn 及 lang=zh-cmn的区别