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.& ...
随机推荐
- 【英语】Bingo口语笔记(17) - 表示“感谢/不用客气“
- Java中ThreadLocal的深入理解
官方对ThreadLocal的描述: "该类提供了线程局部(thread-local)变量.这些变量不同于它们的普通对应物,因为访问某个变量(通过其get或set方法)的每个线程都有自己的局 ...
- jquery ajax jsonp跨域调用实例代码
今天研究了AJAX使用JSONP进行跨域调用的方法,发现使用GET方式和POST方式都可以进行跨域调用,这里简单分享下,方便需要的朋友 客户端代码 复制代码 代码如下: <%@ Page Lan ...
- java 地址记录
java在线API地址 http://docs.oracle.com/javase/7/docs/api/
- 生产环境服务CPU高问题分析
问题描述: 现网个别时候会出现CPU突然飙高的现象,飙高后不能恢复正常. 分析过程: CPU飙高后抓dump,最好本机看,其它机器看dump可能需要下载服务运行机器的sos,clr 0:000 ...
- Selenium2Library系列 keywords 之 _SelectElementKeywords 之 select_all_from_list(self, locator)
def select_all_from_list(self, locator): """Selects all values from multi-select list ...
- jquery小技巧汇总 持续更新中
1.jquery高亮当前选中菜单 $("document").ready(function(){ $(".menu li").click(function(){ ...
- C#判断程序是由Windows服务启动还是用户启动
在Windows系统做网络开发,很多时候都是使用Windows服务的模式,但在调度阶段,我们更多的是使用控制台的模式.在开发程序的时候,我们在Program的Main入口进行判断.最初开始使用Envi ...
- Java 面向对象概念
Interface 接口 An interface defines a protocol of communication between two objects. An interface decl ...
- MemoryMappingFile泄漏分析过程
最近项目突然收到了一个紧急的问题报告 - 用户在进行某些关键操作的时候整个软件突然就crash掉了.幸好产品继承了自动抓取dump的功能... 收到dump之后,通过windbg打开,查看相应的c ...