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组 ...
随机推荐
- storm - 使用过程中的一点思考
引子 这几天为了优化原有的数据处理框架,比较系统的学习了storm的一些内容,整理一下心得 1. storm提供的是一种数据处理思想,它不提供具体的解决方案 storm的核心是topo的定义,而top ...
- Unity3d插件推荐
2D_Toolkit_1.51 动画开发插件包 FingerGestures 触摸插件 ORK_Okashi_RPG_Kit Unity3D角色扮演游戏开发工具包 uScript-Visual-Scr ...
- CodeForces Round #290 Div.2
A. Fox And Snake 代码可能有点挫,但能够快速A掉就够了. #include <cstdio> int main() { //freopen("in.txt&quo ...
- [.NET MVC进阶系列0x] EF Code First 数据迁徙(Migrations)
[因] Entity Framework中使用Code First模式进行开发时,数据库是基于Models中的类自动生成的(生成时间:第一次运行MVC项目时), 每次更改Models中类结构,重新编译 ...
- Java [leetcode 25]Reverse Nodes in k-Group
题目描述: Given a linked list, reverse the nodes of a linked list k at a time and return its modified li ...
- 如何卸载eclipse中的ADT
卸载ADT的方法,方法如下: 1.选择 Help > Install New Software: 2.在"Details" 面板中, 点击"What is alre ...
- Button 自定义(一)-shape
需求:自定义Button,使用系统自定义Shape: 效果图: 1.默认状态 2.选中状态 实现分析: 1.目录结构: 代码实现: 1.button_normal.xml <?xml versi ...
- 单点登录技术:微软Passport单点登录协议和自由联盟规范
随着互联网络应用的普及,越来越多的人开始使用互联网上提供的服务.然而目前提供服务的网站大多采用用户名.口令的方式来识别用户身份,这使得用户需要经常性的输入自己的用户名.口令.显然这种认证方式存在着弊端 ...
- [selenium webdriver Java]检查元素是否存在
Selenium WebDriver没有实现Selenium RC的isElementPresent()方法来检查页面上的元素是否存在. 在WebDriver中封装一个类似的方法,如下: public ...
- 屏蔽同步(JAVA)
以全球气候预测程序为例.这些程序通过将地球分为许多单元,在每个循环中,每个单元的计算都是隔离进行的,直到这些值趋于稳定,然后相邻单元之间就会交换一些数据.所以,从本质上讲,在每个循环中各个线程都必须等 ...