首先我们需要在主布局文件中 放一个 容器,方便让fragment加入进去,我们创建了四个Fragment,并用RedioButton实现了导航栏

MainActivity.java

 package com.example.administrator.fragmentdemo;

 import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton; public class MainActivity extends Activity implements View.OnClickListener { private RadioButton image1;
private RadioButton image2;
private RadioButton image3;
private RadioButton image4; private FirstFragment firstFragment;
private SecondFragment secondFragment;
private ThirdFragment thirdFragment;
private FourFragment fourFragment; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initViews();
initEvents();
//首先 我们先选定一个
select(0);
}
//初始化 各种个 View
private void initViews(){
image1 = (RadioButton) findViewById(R.id.tab_image1);
image2 = (RadioButton) findViewById(R.id.tab_image2);
image3 = (RadioButton) findViewById(R.id.tab_image3);
image4 = (RadioButton) findViewById(R.id.tab_image4);
}
//初始化 监听事件
private void initEvents(){
image1.setOnClickListener(this);
image2.setOnClickListener(this);
image3.setOnClickListener(this);
image4.setOnClickListener(this);
}
// 初始化 各种图片
private void initImageBack(){
image1.setBackgroundResource(R.drawable.chatting_biaoqing_btn_normal);
image2.setBackgroundResource(R.drawable.lbs_icon_disable);
image3.setBackgroundResource(R.drawable.scan_book);
image4.setBackgroundResource(R.drawable.scan_word);
}
//
private void select(int i){
FragmentManager fm = getFragmentManager(); //获得Fragment管理器
FragmentTransaction ft = fm.beginTransaction(); //开启一个事务 hidtFragment(ft); //先隐藏 Fragment switch (i){
case 0:
image1.setBackgroundResource(R.drawable.chatting_biaoqing_btn_enable);
if (firstFragment == null){
firstFragment = new FirstFragment();
ft.add(R.id.fragment_container,firstFragment);
}else{
ft.show(firstFragment);
}
break;
case 1:
image2.setBackgroundResource(R.drawable.lbs_icon_enable);
if (secondFragment == null){
secondFragment = new SecondFragment();
ft.add(R.id.fragment_container,secondFragment);
}else {
ft.show(secondFragment);
}
break;
case 2:
image3.setBackgroundResource(R.drawable.scan_book_hl);
if (thirdFragment == null){
thirdFragment = new ThirdFragment();
ft.add(R.id.fragment_container,thirdFragment);
}else {
ft.show(thirdFragment);
}
break;
case 3:
image4.setBackgroundResource(R.drawable.scan_word_hl);
if(fourFragment == null){
fourFragment = new FourFragment();
ft.add(R.id.fragment_container,fourFragment);
}else {
ft.show(fourFragment);
}
break;
}
ft.commit(); //提交事务
}
//隐藏所有Fragment
private void hidtFragment(FragmentTransaction fragmentTransaction){
if (firstFragment != null){
fragmentTransaction.hide(firstFragment);
}
if (secondFragment != null){
fragmentTransaction.hide(secondFragment);
}
if (thirdFragment != null){
fragmentTransaction.hide(thirdFragment);
}
if (fourFragment != null){
fragmentTransaction.hide(fourFragment);
}
}
//重写监听
@Override
public void onClick(View v) { initImageBack(); //初始化 图片背景 switch (v.getId()){
case R.id.tab_image1:
select(0);
break;
case R.id.tab_image2:
select(1);
break;
case R.id.tab_image3:
select(2);
break;
case R.id.tab_image4:
select(3);
break;
}
}
}

主布局文件,在这里我分开写的,底部的导航栏有新建了一个xml文件,并在主布局文件中用include将他包含进来。

activity_main.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"> <ImageView
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/friendactivity_comment_frame_pressed"/> <FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout> <include layout="@layout/activity_main_tab_view"/> </LinearLayout>

底部导航栏的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
android:background="@drawable/friendactivity_comment_frame_pressed"> <RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"> <RadioButton
android:id="@+id/tab_image1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:background="@drawable/chatting_biaoqing_btn_normal"/> <RadioButton
android:id="@+id/tab_image2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:background="@drawable/lbs_icon_disable"/>
<RadioButton
android:id="@+id/tab_image3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:background="@drawable/scan_book"/>
<RadioButton
android:id="@+id/tab_image4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:background="@drawable/scan_word"/>
</RadioGroup> </LinearLayout>

四个fragment都一样,我就放一个代码,布局也很简单,就放了一个TextView

Fragment.java

package com.example.administrator.fragmentdemo;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* Created by Administrator on 2015/9/3.
*/
public class FirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.first_fragment_view,container,false);
}
}

该fragment的布局文件为:

<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is frist fragment"/>
</LinearLayout>

效果图:

Fragment+RadioButton实现点击切换页面效果的更多相关文章

  1. VUE 实现tab切换页面效果

    一 163邮箱登录tab切换 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  2. Android切换页面效果的实现二:WebView+ViewPager

    前言: 由于第一种切换页面的效果不能满足项目的要求,于是又找到另外一种更简单好用的方法来实现,顿时感觉,做项目开发,找到一种合适的方法能够减少很多时间,(刚开始自己弄的时候还想着自己写手势识别的方法呢 ...

  3. Android切换页面效果的实现一:WebView+ViewFlipper

    前言: 这两周在帮学校做一个新生入学用的“新里程”的项目,要做到页面切换阅读的效果,自己百度了下,找到普遍是使用WebView+ViewFlipper的实现方法,但这种方法不能满足我的要求,因为它很难 ...

  4. 关于Vue中,checkBox等组件在赋值后,点击切换页面未及时更新问题

    我们经常碰到这样的问题,在v-for循环中,给某些组件(此处以checkBox为例)赋值后,组件并不能正常切换, 这是因为数据层太多,render函数没有自动更新,需手动强制刷新. 解决方法:在切换c ...

  5. html+js(swiper.js)+css左右滑动切换页面效果,适配移动端

    demo: 截图: 结构:1.swiper-progress.html2.css文件夹 -swiper.css -swiper.min.css 3.js文件夹 -swiper.min.js -swip ...

  6. Android——Fragment实例精讲——底部导航栏+ViewPager滑动切换页面

    说明: 实现效果: 1- 用ViewPager实现Fragmen之间的切换 2- 底部用RadioGroup实现,更方便的实现图片和字体颜色的改变,更方便的通过RadioButton的点击事件来控制页 ...

  7. react实现页面切换动画效果

    一.前情概要 注:(我使用的路由是react-router4)     如下图所示,我们需要在页面切换时有一个过渡效果,这样就不会使页面切换显得生硬,用户体验大大提升:     but the 问题是 ...

  8. 【转】 Pro Android学习笔记(四二):Fragment(7):切换效果

    目录(?)[-] 利用setTransition 利用setCustomAnimations 通过ObjectAnimator自定义动态效果 程序代码的编写 利用fragment transactio ...

  9. 基于vue2.0打造移动商城页面实践 vue实现商城购物车功能 基于Vue、Vuex、Vue-router实现的购物商城(原生切换动画)效果

    基于vue2.0打造移动商城页面实践 地址:https://www.jianshu.com/p/2129bc4d40e9 vue实现商城购物车功能 地址:http://www.jb51.net/art ...

随机推荐

  1. Django中级篇之模板语言

    模板 一.引用变量 模版的创建过程,对于模版,其实就是读取模版(其中嵌套着模版标签),然后将 Model 中获取的数据插入到模版中,最后将信息返回给用户. {{ xxx }} 二.标签 用{%  %} ...

  2. 第4章 awk编程

    1 awk编程模型       2 awk用法 调用awk有三种方法(与sed类似): 在Shell命令行输入命令调用awk,格式为: awk [-F 域分隔符] 'awk程序段' 输入文件 将awk ...

  3. WinDbg调试CPU占用高的问题 试验+实战 《第七篇》

    一.High CPU试验 1.示例代码 static void Main(string[] args) { Console.Clear(); Console.WriteLine("到命令行下 ...

  4. Qt Meta Object System-元对象系统

    研一的时候开始使用Qt,感觉用Qt开发图形界面比MFC的一套框架来方便的多.后来由于项目的需要,也没有再接触Qt了.现在要重新拾起来,于是要从基础学起. Now,开始学习Qt事件处理机制. 元对象系统 ...

  5. 织梦DedeCMS列表摘要 description 长度控制方法

    [field:description /]标签如何限制字数? [field:description function='cn_substr(@me,80)'/] DedeCMS 里的所有标记都支持这样 ...

  6. 【Qt 】QSettings写ini配置文件

    QSettings写ini配置文件(Qt版本5.2): #include "inidemo.h" #include <QSettings> #include <Q ...

  7. Jquery入门之----------选择器-------------

    Jquery最核心的组成部分就是选择器引擎.他继承了CSS的语法,可以对DOM元素的标签名.属性名.状态等进行快速.精确的选择,并且不必担心浏览器的兼容性.Jquery选择器除实现了基本的标签选择外, ...

  8. u-boot-1.1.6移植之dm9000

    网卡dm9000的执行过程(u-boot版本:u-boot-1.1.6): 在board.c里面有eth_initialize(gd->bd); eth_initialize的实现在eth.c里 ...

  9. jansen字符串转换为xml格式

    /// <summary> /// json字符串转换为Xml对象 /// </summary> /// <param name="sJson"> ...

  10. 调试工具-fiddler

    本地资源替换线上调试 Fiddler是一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网 之间的http通讯,设置断点,查看所有的“进出”Fiddler的数据(指cookie,html ...