流行的应用的导航一般分为两种,一种是底部导航,一种是侧边栏。

我所做的项目涉及到比较多的是底部导航,今天我就把项目中使用的一种实现方式分享一下。

主要实现思路是:在一个Activity里面底部添加四个按键,上边通过切换不同的Fragment来实现。

首先在activity_main.xml

实现一个底部布局

   <RelativeLayout
android:id="@+id/aboveLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#dbdbdb"
android:orientation="vertical" > <LinearLayout
android:id="@+id/fragmentRoot"
android:layout_width="fill_parent"
android:layout_height="fill_parent" /> <LinearLayout
android:id="@+id/bottomList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@android:color/black"
android:orientation="horizontal" > <LinearLayout
android:id="@+id/bottomItemCurrentBg1"
style="@style/main_bottom_item" > <RadioButton
android:id="@+id/rbMain"
style="@style/footbar"
android:drawableTop="@drawable/tab_weixin_normal"
android:paddingTop="8dp"
android:text="微信" />
</LinearLayout> <LinearLayout
android:id="@+id/bottomItemCurrentBg2"
style="@style/main_bottom_item" > <RadioButton
android:id="@+id/rbCategory"
style="@style/footbar"
android:drawableTop="@drawable/tab_address_normal"
android:paddingTop="8dp"
android:text="通讯录" />
</LinearLayout> <LinearLayout
android:id="@+id/bottomItemCurrentBg3"
style="@style/main_bottom_item" > <RadioButton
android:id="@+id/rbFind"
style="@style/footbar"
android:drawableTop="@drawable/tab_find_frd_normal"
android:paddingTop="8dp"
android:text="发现" />
</LinearLayout> <LinearLayout
android:id="@+id/bottomItemCurrentBg4"
style="@style/main_bottom_item" > <RadioButton
android:id="@+id/rbMe"
style="@style/footbar"
android:drawableTop="@drawable/tab_settings_normal"
android:paddingTop="8dp"
android:text="我" />
</LinearLayout>
</LinearLayout>
</RelativeLayout> </RelativeLayout>

这里主要用到的知识点有:style的使用,Radiobutton的一些特殊属性设置,权重,以及drawableTop的使用。

底部布局实现之后,就需要在MainActivity里面找到对应的控件做出相应的逻辑处理了。

首先实例化一个FragmentManager实例

然后初始化首页的布局,这里为微信首页面,通过调用initFragment();来实现,接着就是点击的处理dealBottomButtonsClickEvent();

代码如下:

	/**
* 初始化首个Fragment
*/
private void initFragment() {
FragmentTransaction ft = fMgr.beginTransaction();
WeiXinFragment weiXinFragment = new WeiXinFragment();
ft.add(R.id.fragmentRoot, weiXinFragment, "weiXinFragment");
ft.addToBackStack("weiXinFragment");
ft.commit();
}
/**
* 处理底部点击事件
*/
private void dealBottomButtonsClickEvent() {
findViewById(R.id.rbWeiXin).setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(fMgr.findFragmentByTag("weiXinFragment")!=null && fMgr.findFragmentByTag("weiXinFragment").isVisible()) {
return;
}
popAllFragmentsExceptTheBottomOne(); }
});

点击初始化的代码我只截取了一个,这里用到的几个知识点是:

1、Fragment的管理,添加,放入堆栈,提交

2、关于Fragment的Tag的使用,因为在初始化的时候设置了tag,所以在点击时间里面,判断如果当前的的Fragment显示着,那么就直接return,都则弹出之前所有的Fragment保留WeiXinFragment。

3、关于Fragment堆栈的问题,添加的时候是把一new的Fragment添加到堆栈里,这里调用了fMgr.popBackStack();

最后就是拦截返回键的处理了。

	//点击返回按钮
@Override
public void onBackPressed() {
if(fMgr.findFragmentByTag("weiXinFragment")!=null && fMgr.findFragmentByTag("weiXinFragment").isVisible()) {
MainActivity.this.finish();
} else {
super.onBackPressed();
}
}

逻辑是:如果当前的Fragment是WeiXinFragment则推出当前应用,否则调用父返回键,这样可以保证别的Fragment切换到WeixinFragment

代码里涉及到的别的就是com.walker.fragmentnavigation.fragment包里的四个页面布局,都是继承自Fragment。

需要说明的知识点有:

1、onCreateView里面返回一个View,这个每个Fragment里面都使用了对于的布局

2、布局里面的顶部title是复用的<include layout="@layout/top_title" />

3、在Fragment里面有个getView()的方法可以找到对应的布局控件,然后修改

((TextView)getView().findViewById(R.id.tvTop)).setText("通讯录");

注:用到了腾讯的几张图片

这个例子到这里就结束了,代码下载

【简单项目框架一】Fragment实现的底部导航的更多相关文章

  1. asp.net mvc 简单项目框架的搭建(二)—— Spring.Net在Mvc中的简单应用

    摘要:上篇写了如何搭建一个简单项目框架的上部分,讲了关于Dal和Bll之间解耦的相关知识,这篇来把后i面的部分说一说. 上篇讲到DbSession,现在接着往下讲. 首先,还是把一些类似的操作完善一下 ...

  2. 二、Fragment+RadioButton实现底部导航栏

    在App中经常看到这样的tab底部导航栏   那么这种效果是如何实现,实现的方式有很多种,最常见的就是使用Fragment+RadioButton去实现.下面我们来写一个例子 首先我们先在activi ...

  3. asp.net mvc 简单项目框架的搭建过程(一)对Bll层和Dal层进行充分解耦

    学习asp.net 已经有近三个月的时间了,在asp.net mvc上花的时间最多,但个人真是有些菜,不得不说,asp.net mvc的水真的还是蛮深的.目前在公司实习,也见过公司几个项目的代码了.对 ...

  4. 使用vue-cli脚手架搭建简单项目框架

    1.首先已经安装了node,最好版本6以上. 2.安装淘宝镜像 大家都知道国内直接使用 npm 的官方镜像是非常慢的,这里推荐使用淘宝 NPM 镜像.这样就可以直接使用cnpm了. npm insta ...

  5. Android Fragment实现微信底部导航

    1.XML布局 (1)主界面 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout x ...

  6. Android商城开发系列(三)——使用Fragment+RadioButton实现商城底部导航栏

    在商城第一篇的开篇当中,我们看到商城的效果图里面有一个底部导航栏效果,如下图所示: 今天我们就来实现商城底部导航栏,最终效果图如下所示:   那么这种效果是如何实现,实现的方式有很多种,最常见的就是使 ...

  7. Flutter学习三之搭建一个简单的项目框架

    上一篇文章介绍了Dart的语法的基本使用,从这篇文章开始,开发一个基于玩Android网站的app.使用的他们开放的api来获取网站数据. 根据网站的结构,我们app最外层框架需要添加一个底部导航栏, ...

  8. Django web框架 下载安装 简单项目搭建

    什么是web应用? Web应用程序是一种可以通过Web访问的应用程序,程序的最大好处是用户很容易访问应用程序,用户只需要有浏览器即可,不需要再安装其他软件 应用程序有两种模式C/S.B/S.C/S是客 ...

  9. 01 uni-app框架学习:项目创建及底部导航栏tabBar配置

    1.创建一个项目类型选择uniapp 2. pages里新建3个页面如下 3.在pages.json中配置底部导航tabBar 效果展示:

随机推荐

  1. FWA winner | Car Visualizer WebGL

    FWA winner | Car Visualizer WebGL Car Visualizer made in WebGL using ThreeJS. It requires a modern b ...

  2. MSSQL 常用内置函数

    一.判断表是否存在 IF objectproperty(object_id(@tableName),'IsUserTable') IS NOT NULL PRINT '存在' ELSE PRINT ' ...

  3. Handshakes(思维) 2016(暴力)

    Handshakes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Sta ...

  4. Yeslab现任明教教主数据中心第二门课程UCS 视频教程下载

    Yeslab现任明教教主数据中心第二门课程UCS 视频教程下载 视频教程目录 Yeslab现任明教教主数据中心第二门课程UCS.1.介绍UCS.rar Yeslab现任明教教主数据中心第二门课程UCS ...

  5. H5 视频直播相关技术

    一.移动视频直播发展 大家首先来看下面这张图: 可以看到,直播从 PC 到一直发展到移动端,越来越多的直播类 App 上线,同时移动直播进入了前所未有的爆发阶段,但是对于大多数移动直播来说,还是要以 ...

  6. 基于angularJS和requireJS的前端架构

    1.概要描述 1.1.angularJS描述:angularJS是可以用来构建WEB应用的,WEB应用中的一种端对端的完整解决方案.通过开发者呈现一个更高层次的抽象来简化应用的开发.最适合的就是用它来 ...

  7. Deppon接口开发

    一.1)  支持的传输协议  http ,暂时只支持HTTP协议进行通信. (2) 支持的数据传输格式  Json  ,所有接口暂只支持json消息格式. (3) 编码格式:UTF-8   交互编码格 ...

  8. Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  9. CDZSC_2015寒假新人(1)——基础 f

    Description An inch worm is at the bottom of a well n inches deep. It has enough energy to climb u i ...

  10. $digest already in progress

    最近在写项目时经常遇到一个小问题,在上下文外改变视图,通常来说我们需要apply()便可以实现,问题是加了apply()后控制台报错:$digest already in progress:随后我把a ...