Fragment在实际项目开发中使用的越来越多,如今简介一下

布局文件:

<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" > <FrameLayout
android:id="@+id/layout_content"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1.0" /> <RadioGroup
android:id="@+id/main_radio"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
android:paddingTop="8dp"
android:background="@drawable/bottom_tab_bg"
android:gravity="center_vertical"
android:orientation="horizontal" > <RadioButton
android:id="@+id/rb_1"
style="@style/main_tab_bottom"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:drawableTop="@drawable/icon_function" /> <RadioButton
android:id="@+id/rb_2"
style="@style/main_tab_bottom"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:drawableTop="@drawable/icon_newscenter" /> <RadioButton
android:id="@+id/rb_3"
style="@style/main_tab_bottom"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:drawableTop="@drawable/icon_govaffairs" /> <RadioButton
android:id="@+id/rb_4"
style="@style/main_tab_bottom"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:drawableTop="@drawable/icon_setting" /> <RadioButton
android:id="@+id/rb_5"
style="@style/main_tab_bottom"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:drawableTop="@drawable/icon_smartservice" />
</RadioGroup> </LinearLayout>

Java文件:

package com.succ7.fragmenttest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener; public class MainActivity extends FragmentActivity { private FrameLayout layout_content;
/**
* 默认显示的fragment
*/
private int index = 0;
private int current_index = R.id.rb_1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main); layout_content = (FrameLayout) findViewById(R.id.layout_content);
RadioGroup main_radio = (RadioGroup) findViewById(R.id.main_radio); main_radio.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rb_1:
index = 0;
break;
case R.id.rb_2:
index = 1;
break;
case R.id.rb_3:
index = 2;
break;
case R.id.rb_4:
index = 3;
break;
case R.id.rb_5:
index = 4;
break;
}
//这个是初始化fragment
Fragment fragment = (Fragment) fragments.instantiateItem(layout_content, index);
//默认选择第0个fragment
fragments.setPrimaryItem(layout_content, 0, fragment);
//fragment提交更新事务
fragments.finishUpdate(layout_content);
}
}); main_radio.check(current_index);
} // getSupportFragmentManager() 这种方法是在FragmentActivity 里面的
FragmentStatePagerAdapter fragments = new FragmentStatePagerAdapter(
getSupportFragmentManager()) { //几个fragment就返回几个
@Override
public int getCount() {
return 5;
} @Override
public Fragment getItem(int arg0) {
Fragment fragment = null;
switch (arg0) {
case 0:
fragment = new Fragment1();
break;
case 1:
fragment = new Fragment2();
break;
case 2:
fragment = new Fragment3();
break;
case 3:
fragment = new Fragment4();
break;
case 4:
fragment = new Fragment5();
break;
}
return fragment;
}
};
}

Fragment1,Fragment2-5代码:

package com.succ7.fragmenttest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView; public class Fragment1 extends Fragment { @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ImageView imageview = new ImageView(getActivity());
imageview.setBackgroundResource(R.drawable.g1);
return imageview;
} @Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
} @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
} @Override
public void setMenuVisibility(boolean menuVisible) {
super.setMenuVisibility(menuVisible); if(getView() != null){
getView().setVisibility(menuVisible?View.VISIBLE:View.INVISIBLE);
}
/*if(menuVisible){
getView().setVisibility(View.VISIBLE);
}else{
getView().setVisibility(View.INVISIBLE);
}*/
} }

Fragment的使用简单介绍【Android】的更多相关文章

  1. 简单介绍Android应用特色及详解四大组件

    Android应用特色 Android主要有什么特色呢,有以下几个方面来体现: 四大组件 丰富的系统控件 SQLite数据库等持久化技术 地理位置定位 强大的多媒体 传感器 1,四大组件 Androi ...

  2. Android(Lollipop/5.0) Material Design(一) 简单介绍

    Material Design系列 Android(Lollipop/5.0)Material Design(一) 简单介绍 Android(Lollipop/5.0)Material Design( ...

  3. Fragment滑动切换简单案例

    Fragment的产生与介绍Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视.针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套App,然后拷贝一份,修改布局以适应平板神 ...

  4. android复习第一天-----简单的android常识

    前言:要去面试了,这些天花一些事件把android中简单的知识点来串联的复习一下 1,android中的工程结构 src文件夹:存储android文件的源代码 gen文件夹:有工具自动生成,不要去修改 ...

  5. Rebound动画框架简单介绍

    Rebound动画框架简单介绍 Android菜鸟一枚,有不对的地方希望大家指出,谢谢. 最近在接手了一个老项目,发现里面动画框架用的是facebook中的Rebound框架,由于以前没听说过,放假时 ...

  6. Android Fragment(三)ListFragment简单介绍以及Fragment之间通信

    一.Fragment通信简单介绍:Fragments之间是不能够直接通信的,他们之间的通信是通过Activity这个中间件来通信的, 为了让Fragment跟它的Activity通信,我们可以在Fra ...

  7. 使用Kotlin开发Android应用(I):简单介绍

    使用Kotlin开发Android应用(I):简单介绍 @author ASCE1885的 Github 简书 微博 CSDN 原文链接 Kotlin是一门基于JVM的编程语言.它正成长为Androi ...

  8. ANDROID培训准备资料之四大组件的简单介绍

    Android四大组件是一个android app 最基本的组成部分,这篇博客主要给大家简单的介绍一下四种组件 (1)Activities (2)Services (3)BroadcastReceiv ...

  9. Android开发自学笔记(Android Studio)—4.界面编程与View组件简单介绍

    一.引言 Android应用开发最重要的一份内容就是界面的开发,无论你程序包含的内容多么优秀,如若没有一个良好的用户交互界面,最终也只是会被用户所遗弃.Android SDK提供了大量功能丰富的UI组 ...

随机推荐

  1. bzoj1084: [SCOI2005]最大子矩阵

    dp.状态转移方程在代码里 #include<cstdio> #include<algorithm> #include<cstring> using namespa ...

  2. HNOI2008明明的烦恼

    写的很好的题解:http://www.cnblogs.com/zhj5chengfeng/archive/2013/08/23/3278557.html 我这种蒻蒻什么都不会啊…… 代码:(copy的 ...

  3. UVA 1349 Optimal Bus Route Design 最优公交路线(最小费用流,拆点)

    题意: 给若干景点,每个景点有若干单向边到达其他景点,要求规划一下公交路线,使得每个景点有车可达,并且每个景点只能有1车经过1次,公车必须走环形回到出发点(出发点走2次).问是否存在这样的线路?若存在 ...

  4. SharePoint 2010 获取列表中所有数据(包括文件夹内)的方法

    转: http://blog.csdn.net/wangwenism/article/details/8751411 SharePoint的列表能存储文件以及文件夹,用户使用的时候,经常会建几个分类文 ...

  5. DICOM医学图像处理:DIMSE消息发送与接收“大同小异”之DCMTK fo-dicom mDCM

    背景: 从DICOM网络传输一文开始,相继介绍了C-ECHO.C-FIND.C-STORE.C-MOVE等DIMSE-C服务的简单实现,博文中的代码给出的实例都是基于fo-dicom库来实现的,原因只 ...

  6. 积和式Permanent在Mathematica中的运算

    Permanent[m_List] := With[{v = Array[x, Length[m]]}, Coefficient[Times @@ (m.v), Times @@ v]] 参考资料:  ...

  7. 明修栈道,暗渡陈仓----之私募一哥徐翔新玩法 z

    前言:去年以来,因徐翔和宁电突然举牌资质平平的 000692 惠天热电,引起本人的兴趣,陆陆续续花了比较多的时间和精力去研究和跟踪000692惠天热电,期间也两次亲自去沈阳调研,从一些台前幕后人士那里 ...

  8. Travel(HDU 4284状压dp)

    题意:给n个城市m条路的网图,pp在城市1有一定的钱,想游览这n个城市(包括1),到达一个城市要一定的花费,可以在城市工作赚钱,但前提有工作证(得到有一定的花费),没工作证不能在该城市工作,但可以走, ...

  9. 《Python基础教程(第二版)》学习笔记 -> 第三章 使用字符串

    本章讲话介绍如何使用字符串格式化其他的值,并简单了解一下利用字符串的分割.联接.搜索等方法能做些什么. 基本字符串操作 所有标准的序列操作(索引.分片.乘法.判断成员资格.求长度.取最大最小值)对字符 ...

  10. 【暑假】[实用数据结构]UVAlive 4329 Ping pong

    UVAlive 4329 Ping pong 题目: Ping pong Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: % ...