Fragment的使用简单介绍【Android】
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】的更多相关文章
- 简单介绍Android应用特色及详解四大组件
Android应用特色 Android主要有什么特色呢,有以下几个方面来体现: 四大组件 丰富的系统控件 SQLite数据库等持久化技术 地理位置定位 强大的多媒体 传感器 1,四大组件 Androi ...
- Android(Lollipop/5.0) Material Design(一) 简单介绍
Material Design系列 Android(Lollipop/5.0)Material Design(一) 简单介绍 Android(Lollipop/5.0)Material Design( ...
- Fragment滑动切换简单案例
Fragment的产生与介绍Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视.针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套App,然后拷贝一份,修改布局以适应平板神 ...
- android复习第一天-----简单的android常识
前言:要去面试了,这些天花一些事件把android中简单的知识点来串联的复习一下 1,android中的工程结构 src文件夹:存储android文件的源代码 gen文件夹:有工具自动生成,不要去修改 ...
- Rebound动画框架简单介绍
Rebound动画框架简单介绍 Android菜鸟一枚,有不对的地方希望大家指出,谢谢. 最近在接手了一个老项目,发现里面动画框架用的是facebook中的Rebound框架,由于以前没听说过,放假时 ...
- Android Fragment(三)ListFragment简单介绍以及Fragment之间通信
一.Fragment通信简单介绍:Fragments之间是不能够直接通信的,他们之间的通信是通过Activity这个中间件来通信的, 为了让Fragment跟它的Activity通信,我们可以在Fra ...
- 使用Kotlin开发Android应用(I):简单介绍
使用Kotlin开发Android应用(I):简单介绍 @author ASCE1885的 Github 简书 微博 CSDN 原文链接 Kotlin是一门基于JVM的编程语言.它正成长为Androi ...
- ANDROID培训准备资料之四大组件的简单介绍
Android四大组件是一个android app 最基本的组成部分,这篇博客主要给大家简单的介绍一下四种组件 (1)Activities (2)Services (3)BroadcastReceiv ...
- Android开发自学笔记(Android Studio)—4.界面编程与View组件简单介绍
一.引言 Android应用开发最重要的一份内容就是界面的开发,无论你程序包含的内容多么优秀,如若没有一个良好的用户交互界面,最终也只是会被用户所遗弃.Android SDK提供了大量功能丰富的UI组 ...
随机推荐
- EDM排版table设置padding在ie7下bug
今天搞EDM发现一个在ie7下很扯淡的bug,由于以前没遇到过,所以花了点时间来解决下. IE7下bug重现: <table cellpadding="0" cellspac ...
- asp mvc 路由
public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Workflo ...
- 【待填坑】bzoj上WC的题解
之前在bzoj上做了几道WC的题目,现在整理一下 bzoj2115 去膜拜莫队的<高斯消元解xor方程组> bzoj2597 LCT维护MST bzoj1758 分数规划+树分治+单调队列 ...
- poj3321
树映射到树状数组上 非常好的题目,给了我很多启发 题目要求动态求一个棵子树的节点个数 不禁联想到了前缀和,只要我们能用一个合适的优先级表示每个顶点,那么就好做了 我们可以考虑将子树表示成区间的形式 这 ...
- 让Windows Server 2008 + IIS 7+ ASP.NET 支持10万个同时请求
具体设置如下: 1. 调整IIS 7应用程序池队列长度 由原来的默认1000改为65535. IIS Manager > ApplicationPools > Advanced Setti ...
- UEFI GPT
其实关于UEFI的几篇文章很早就写下了,只是自己读了一遍感觉很不满意,就决定重写.目的是想用最简单直白的语言把内容写出来,让每个人都能轻松读懂.当然,如果你已经对这些内容有了很深的理解的话,这篇文章除 ...
- 新手学习数据库(一)用Powerdesigner设计数据库
说明: 一.学会用开发语言进行数据库编程,其关键是在于学会sql语言,开发语言只不过给程序员提供了一个操作数据库的接口罢了. 二. 本人也是初学者,采用的数据库设计软件是powerdesigner.利 ...
- MySQL table_id原理及风险分析
1. 什么是table_id MySQL binlog文件按格式分为文件头部和事件信息.文件头部占4字节,内容固定为:"\xfe\x62\x69\x6e",接下来就是各个event ...
- [转] windows7 IIS管理器 在计算机“.”上没有找到WAS服务
原文地址:windows7 IIS管理器 在计算机"."上没有找到WAS服务作者:云中的风 OS:windows7旗舰版 产生问题原因:运行金蝶K3-HR时客户端提示中间层服务器不 ...
- SQL中的CASE WHEN用法
其语法如下: 1)case vlaue when [compare-value]then reslut [when[compare-value]] then result ...] [else res ...