Fragment+RadioButton实现点击切换页面效果
首先我们需要在主布局文件中 放一个 容器,方便让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实现点击切换页面效果的更多相关文章
- VUE 实现tab切换页面效果
一 163邮箱登录tab切换 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- Android切换页面效果的实现二:WebView+ViewPager
前言: 由于第一种切换页面的效果不能满足项目的要求,于是又找到另外一种更简单好用的方法来实现,顿时感觉,做项目开发,找到一种合适的方法能够减少很多时间,(刚开始自己弄的时候还想着自己写手势识别的方法呢 ...
- Android切换页面效果的实现一:WebView+ViewFlipper
前言: 这两周在帮学校做一个新生入学用的“新里程”的项目,要做到页面切换阅读的效果,自己百度了下,找到普遍是使用WebView+ViewFlipper的实现方法,但这种方法不能满足我的要求,因为它很难 ...
- 关于Vue中,checkBox等组件在赋值后,点击切换页面未及时更新问题
我们经常碰到这样的问题,在v-for循环中,给某些组件(此处以checkBox为例)赋值后,组件并不能正常切换, 这是因为数据层太多,render函数没有自动更新,需手动强制刷新. 解决方法:在切换c ...
- html+js(swiper.js)+css左右滑动切换页面效果,适配移动端
demo: 截图: 结构:1.swiper-progress.html2.css文件夹 -swiper.css -swiper.min.css 3.js文件夹 -swiper.min.js -swip ...
- Android——Fragment实例精讲——底部导航栏+ViewPager滑动切换页面
说明: 实现效果: 1- 用ViewPager实现Fragmen之间的切换 2- 底部用RadioGroup实现,更方便的实现图片和字体颜色的改变,更方便的通过RadioButton的点击事件来控制页 ...
- react实现页面切换动画效果
一.前情概要 注:(我使用的路由是react-router4) 如下图所示,我们需要在页面切换时有一个过渡效果,这样就不会使页面切换显得生硬,用户体验大大提升: but the 问题是 ...
- 【转】 Pro Android学习笔记(四二):Fragment(7):切换效果
目录(?)[-] 利用setTransition 利用setCustomAnimations 通过ObjectAnimator自定义动态效果 程序代码的编写 利用fragment transactio ...
- 基于vue2.0打造移动商城页面实践 vue实现商城购物车功能 基于Vue、Vuex、Vue-router实现的购物商城(原生切换动画)效果
基于vue2.0打造移动商城页面实践 地址:https://www.jianshu.com/p/2129bc4d40e9 vue实现商城购物车功能 地址:http://www.jb51.net/art ...
随机推荐
- PostMessage与SendMessage的区别(二)
在做基于窗口的Windows程序的时候,我们避免不了要向窗口发送消息,有两种方式,一种是PostMessage,另外一种是SendMessage.关于这两个宏,我是通过狠狠的看MSDN才搞明白的,那里 ...
- 跟我学 NHibernate (一)
NHibernate 是一个强大的 ORM 框架,本博文主要就 NHibernate 的使用方法及语法做一些简单的介绍. 1.NHibernate 语法 新建一个类,命名为: QueryCrit ...
- php 数组转json格式
1.php若为关系数组:转化为由花括号包围的对象: 输入:$test = array("1"=>1,"2"=>2,"3"=> ...
- JavaScript之数组循环 forEach 循环输出数组元素
var arrayAll = []; arrayAll.push(1); arrayAll.push(2); arrayAll[arrayAll.length] = 3; arrayAll[array ...
- Acronis Server备份Linux系统
Acronis Server备份Linux系统 前段时间用Acronis Disk Director Suite解决了Thinkpad笔记本在win7的分区问题(http://chenguang.bl ...
- 数学对象Math ceil()、floor()、round()方法
Math.ceil() 功能:对一个数进行上取整. 语法:Math.ceil(x) 参数: x:一个数值. 返回值:返回大于或等于x,并且与之最接近的整数. 注:如果x是正数,则把小数“入”: ...
- leetcode 100
100. Same Tree Given two binary trees, write a function to check if they are equal or not. Two binar ...
- Oracle定时器执行多线程
what里面加下面代码强制执行多线程 begin execute immediate 'alter session force parallel dml parallel 16'; pkg_s ...
- AnyCAD C++ SDK与OpenCASCADE互操作
AnyCAD SDK有.Net和C++两个版本,使用C++版本的AnyPlatformOcc模块可以实现与OpenCASCADE互操作. C++版本(VS2010 32bit)下载 在AOBridge ...
- 为什么for in循环不适合用于数组
首先一点无关的,使用(var i in a) 而不是( i in a),除非你想创建全局变量. 第二点,for in 循环会忽略空的数组 var a = []; a[5] = 5; // Perfec ...