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. It is indirectly referenced from required .class files

    配置eclipse的导入包,有些包引用的位置不正确

  2. 解决java switch……case不能匹配字符串的问题

    java1.7已经支持了匹配字符串 方案1. enum Animal { dog,cat,bear; public static Animal getAnimal(String animal){ re ...

  3. 陈正冲老师对于c语言野指针的解释

    那到底什么是野指针呢?怎么去理解这个“野”呢?我们先看别的两个关于“野”的词: 野孩子:没人要,没人管的孩子:行为动作不守规矩,调皮捣蛋的孩子.野狗:没有主人的狗,没有链子锁着的狗,喜欢四处咬人. 对 ...

  4. servlet规范

    Servlet规范 一个最基本的 Java Web 项目所需的 jar 包只需要一个 servlet-api.jar ,这个 jar 包中的类大部分都是接口,还有一些工具类,共有 2 个包,分别是 j ...

  5. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.4.1

    Let $x,y,z$ be linearly independent vectors in $\scrH$. Find a necessary and sufficient condition th ...

  6. 与非CCR代码互操作

    导读:CCR可以轻松的承载STA组件或者与它互操作:组件应该创建一个只有一个线程的CCR Dispatcher实例,并且在Dispatcher的构造函数中指定线程套间策略.DispatcherQueu ...

  7. 配置Git自动补全功能

    Git装好后,默认是不会有按tab补全命令的功能的.可如下配置: 去git的源码中,找到contrib/completion/git-completion.bash 将git-completion.b ...

  8. puppet学习:文件夹权限的问题

    之前Zabbix自动部署的文件夹结构总觉得别扭,今天下午抽空调整了下.调整完后,依然是例行的测试. 在客户端执行puppet agent -t时,报错如下: Failed to generate ad ...

  9. <转>SpringMVC与Struts2 比较总结

    原链接:http://blog.csdn.net/chenleixing/article/details/44570681 个人整理: 1.级别不同:SpringMVC :方法  Struts是 类级 ...

  10. activemq p2p方式

    package ch02.chat; import java.io.Serializable; import javax.jms.Connection; import javax.jms.Connec ...