Fragment初步了解
fragment
1.fragment解释:
从英文上来说fragment是碎片和片段的意思,这解释的是相当到位的,因为android中的fragment就像是碎片嵌在了Activity当中的,为构造良好的UI起着至关重要的作用。
fragment和Activity很像,Activity是通过复写onCreate()方法来载入布局文件的,而fragment是通过onCreateView()方法返回一个fragment的布局的。
2.fragment使用前提:
要使用fragment你的SDK版本必须在11以上。在你的Manifest.xml查看版本。
我用的是:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" /> <!-- 要大于11才行 -->
3.下面就通过一个简单的例子来说明fragment的使用方法
MainActivity的class:
public class MainActivity extends FragmentActivity implements OnClickListener { //要继承FragmentActivity噢
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button) findViewById(R.id.btn1); //开第一个fragment
Button btn2 = (Button) findViewById(R.id.btn2); //开第二个fragment
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
System.out.println("第一个button");
FragmentTransaction ft = getSupportFragmentManager() //注意这里是getSupportFragmentManager()不是getFragmentManager() 因为是用了android.support.v4.app不是用了android.support.app
.beginTransaction();
FirstFragment first = new FirstFragment();
ft.replace(R.id.container, first);
ft.commit();
break;
case R.id.btn2:
FragmentTransaction ft2 = getSupportFragmentManager()
.beginTransaction();
SecondFragment second = new SecondFragment();
ft2.replace(R.id.container, second);
ft2.commit();
break;
default:
break;
}
}
}
MianActivity
MainActivity对应的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="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="切换至第一个fragment" /> <Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="切换至第二个fragment" />
</LinearLayout>
<!-- 用来装fragment --> <RelativeLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout> </LinearLayout>
activity_main.xml
FirstFragment的class
public class FirstFragment extends Fragment {
public FirstFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.first_fragment, container, false);
// 转向Activity的button
Button btn1 = (Button) view.findViewById(R.id.button1);
// 开第二个fragment
Button btn2 = (Button) view.findViewById(R.id.button2);
btn1.setOnClickListener(new btn1());
btn2.setOnClickListener(new btn2());
return view;
}
class btn1 implements OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(getActivity(), SecondActivity.class);
startActivity(intent);
}
}
class btn2 implements OnClickListener {
@Override
public void onClick(View v) {
FragmentTransaction ft = getFragmentManager().beginTransaction(); //是getFragmentManager不是getSupportFragmentManager
SecondFragment second = new SecondFragment();
ft.replace(R.id.container, second);
ft.commit();
}
}
}
FirstFragment
FirstFragment所引用的XML 文件
<?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:background="@android:color/darker_gray"
android:gravity="center"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="我是第一个fragment" /> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开一个activity" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="转向第二个fragment" /> </LinearLayout>
first_fragment.xml
SecondFragment的class
public class SecondFragment extends Fragment {
public SecondFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater
.inflate(R.layout.second_fragment, container, false);
Button btn2 = (Button) view.findViewById(R.id.button111);
btn2.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction fm = getFragmentManager()
.beginTransaction();
FirstFragment first = new FirstFragment();
fm.replace(R.id.container, first);
fm.commit();
}
});
return view; //一定要记住放回一个VIEW对象
}
}
SecondFragment
SecondFragment多引用的XML 文件
<?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:background="@android:color/white"
android:gravity="center"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="我是第二个个fragment" /> <Button
android:id="@+id/button111"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="转向第一个fragment" /> </LinearLayout>
second_fragment.xml
SecondActivity的class
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
SecondActivity
SecondActivity对应的XML文件
<?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:background="@android:color/white"
android:gravity="center"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="我是第二个个fragment" /> <Button
android:id="@+id/button111"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="转向第一个fragment" /> </LinearLayout>
second_fragment.xml
Fragment初步了解的更多相关文章
- Android Fragment 初步解析
Fragment经常在我们的开发中见到,但是自我感觉对Fragment的理解还是处于初级的阶段,接下来我将用几篇文章尽量深的解析Fragment 让我们开始吧!!! Fragment的生命周期 Fra ...
- fragment初步认识
- Alpha第六天
Alpha第六天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...
- Alpha冲刺——第六天
Alpha第六天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...
- Mnesia动态添加节点杂记
FAQ List: 1. 如果动态的添加一个节点到Mnesia cluster中 2. 如何动态的从mnesia cluster中删除一个节点 3. 在一个节点上演示将当前已有的表格分片fragmen ...
- Android Fragment使用(四) Toolbar使用及Fragment中的Toolbar处理
Toolbar作为ActionBar使用介绍 本文介绍了在Android中将Toolbar作为ActionBar使用的方法. 并且介绍了在Fragment和嵌套Fragment中使用Toolbar作为 ...
- Fragment详解之三——管理Fragment(1)
相关文章: 1.<Fragment详解之一--概述>2.<Fragment详解之二--基本使用方法>3.<Fragment详解之三--管理Fragment(1)>4 ...
- Android之Fragment学习总结(1)
对于Fragment的学习: 近日初步学习了Fragment这以特殊的组件,其依托与一个Activity,与Activity的生命周期息息相关,为其设置的视图只有当其关联到一个Activity才会起效 ...
- 管理Fragment
转载原地址:http://blog.csdn.net/harvic880925/article/details/44927375 相关文章: 1.<Fragment详解之一——概述>2.& ...
随机推荐
- context-param引发spring容器以及servlet容器的关联
转自:http://blog.csdn.net/liaoxiaohua1981/article/details/6759206 格式定义: [html] view plaincopy <cont ...
- 每天一个Linux命令(7): cp
cp命令 该命令的功能是将给出的文件或目录拷贝到另一文件或目录中,同MSDOS下的copy命令一样,功能十分强大. 语法: cp [选项] 源文件或目录 目标文件或目录 ...
- NSThread小笔记
接口 Initializing an NSThread Object – init – initWithTarget:selector:object: Starting a Thread + deta ...
- 【转】Ubuntu 12.04 安装JDK 8和Eclipse
原文网址:http://blog.csdn.net/yechaodechuntian/article/details/24853813 Ubuntu 12.04 下安装 JDK8 方法一:(缺点是安装 ...
- Codeforces Round #217 (Div. 2) c题
C. Mittens time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Android 中像素px和dp的转化
在Android的布局文件中,往往使用dp作为控件的宽度和高度尺寸,但是在Java代码中,调用getWidth()方法获得的尺寸单位却是像素px,这两个单位有明显的区别:dp和屏幕的密度有关,而px与 ...
- bjfu1287字符串输出的大水题
不多说 /* * Author : ben */ #include <cstdio> #include <cstdlib> #include <cstring> # ...
- Jersey搭建Restful服务器 on Ubuntu
自己试着在Ubuntu上搭建一个 1.首先安装eclipse和tomcat sudo apt-get install eclipse tomcat7 -y 2.把tomcat的group assign ...
- 【Linux】Mutex互斥量线程同步的例子
0.互斥量 Windows下的互斥量 是个内核对象,每次WaitForSingleObject和ReleaseMutex时都会检查当前线程ID和占有互斥量的线程ID是否一致. 当多次Wait**时就 ...
- oracle 定时任务(DBMS_JOB)
示例如下: -- 每隔一分钟执行存储过程p1 create or replace procedure p2 as job_num number; begin dbms_job.submit(job_n ...