Android入门——UI(8)——Fragment(2)
先演示一下如何在一个activity中放置两个Fragment,先定义两个Fragment
<?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:gravity="center"
android:orientation="vertical"> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="注销登陆" />
</LinearLayout>
fragment_indexbottom.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:orientation="vertical"> <TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="上面的片段显示用户信息" />
</LinearLayout>
fragment_indextop.xml
package com.ouc.wkp.ui1; import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* Created by wkp on 2016/8/25.
*/
public class FragmentIndexBottom extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_indexbottom,container,false);
return view;
}
}
FragmentIndexBottom.java
package com.ouc.wkp.ui1; import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; /**
* Created by wkp on 2016/8/25.
*/
public class FragmentIndexTop extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_indextop,container,false);
return view;
}
}
FragmentIndexTop.java
然后定义Activity,放置两个FrameLayout
<?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:orientation="vertical"> <FrameLayout
android:id="@+id/fragment_layout_top"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></FrameLayout> <FrameLayout
android:id="@+id/fragment_layout_bottom"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></FrameLayout>
</LinearLayout>
fragment_demo2.xml
package com.ouc.wkp.ui1; import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction; /**
* Created by wkp on 2016/8/25.
*/
public class FragmentDemo2 extends FragmentActivity { @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_demo2); FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_layout_top, new FragmentIndexTop());
ft.replace(R.id.fragment_layout_bottom, new FragmentIndexBottom());
ft.commit();
}
}
FragmentDemo2.java

上面的例子比较简单,和上一篇基本类似,所以很容易看出使用Fragment是为了代码的复用。
下面介绍fragment和activity之间的通信。有两种形式,一种是fragment->activity,另一种是activity->fragment。
实现方式是定义modify函数或者定义事件监听方法回调。方法回调的方式比较不好理解
<?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="#f00"
android:orientation="vertical"> <TextView
android:id="@+id/tv_in_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是一个Fragment,通过动态方式加载" /> <Button
android:id="@+id/btn_in_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
fragment_index.xml
package com.ouc.wkp.ui1; import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; /**
* Created by wkp on 2016/8/25.
*/
public class FragmentIndex extends Fragment {//app包下3.0以后才可以使用 private OnBtnClickListener onBtnClickListener; public void setOnBtnClickListener(OnBtnClickListener onBtnClickListener) {
this.onBtnClickListener = onBtnClickListener;
} public interface OnBtnClickListener{
void onBtnClick();
} @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//别遗忘第三个false参数,否则会出现 java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
View view=inflater.inflate(R.layout.fragment_index,container,false); view.findViewById(R.id.btn_in_fragment).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// FragmentDemo fragmentDemo=(FragmentDemo) getActivity();
// fragmentDemo.modify();
if(onBtnClickListener!=null){
onBtnClickListener.onBtnClick();
}
}
});
return view;
} public void modify(){
TextView textView=(TextView) getView().findViewById(R.id.tv_in_fragment);
textView.setText("已经修改fragment里面的UI元素的值");
}
}
FragmentIndex.java
<?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:orientation="vertical"> <Button
android:id="@+id/btn_addd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加" /> <Button
android:id="@+id/btn_deletee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="减少" /> <FrameLayout
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="200dp"></FrameLayout> <Button
android:id="@+id/btn_modify"
android:text="修改fragment的界面"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <TextView
android:id="@+id/tv_in_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="哈哈哈"/>
</LinearLayout>
fragment_demo.xml
package com.ouc.wkp.ui1; import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TextView; /**
* Created by wkp on 2016/8/25.
*/
public class FragmentDemo extends FragmentActivity{ FragmentIndex fragmentIndex; @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_demo); fragmentIndex=new FragmentIndex();
//接口回调
fragmentIndex.setOnBtnClickListener(new FragmentIndex.OnBtnClickListener(){ @Override
public void onBtnClick() {
TextView textView=(TextView)findViewById(R.id.tv_in_activity);
textView.setText("修改activity中的textView(通过方法回调)");
}
}); findViewById(R.id.btn_addd).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction(); ft.replace(R.id.frame_layout,fragmentIndex);
ft.commit();
}
}); findViewById(R.id.btn_deletee).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction(); ft.remove(fragmentIndex);
ft.commit();
}
}); findViewById(R.id.btn_modify).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//从Activity修改Fragment的值
fragmentIndex.modify();;
}
});
} public void modify(){
TextView textView=(TextView) findViewById(R.id.tv_in_activity);
textView.setText("修改activity中的textView");
}
}
FragmentDemo.java
Android入门——UI(8)——Fragment(2)的更多相关文章
- Android入门——UI(7)——Fragment
先上fragment静态加载的代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...
- Android入门——UI(9)
SwipRefreshLayout下拉刷新控件 <?xml version="1.0" encoding="utf-8"?> <android ...
- android入门——UI(6)——ViewPager+Menu+PopupWindow
一.使用ViewPager开发新特性引导界面 <?xml version="1.0" encoding="utf-8"?> <Relative ...
- android入门——UI(5)
最近时间实在匆忙,博客的代码基本没有解释. 介绍ExpandableListView <?xml version="1.0" encoding="utf-8&quo ...
- android入门——UI(4)
GridView控件实现菜单 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xml ...
- android入门——UI(3)
Spinner控件 ListView控件 一.Spinner控件 点击Spinner会弹出一个包含所有可选值的dropdown菜单,从该菜单中可以为Spinner选择一个新值. 有两种指定数据源的 ...
- Android入门——UI(2)
介绍SeekBar拖动条控件.ProgressBar进度条控件.DatePicker日历控件.TimePicker时间控件 <?xml version="1.0" encod ...
- android入门——UI(1)
一.使用TextView ImageView Button EditView做出登录页面 <?xml version="1.0" encoding="utf-8&q ...
- Android - 用Fragments实现动态UI - 创建Fragment
你可以把fragment当作activity中的一个活动模块,它有自己的生命周期,自己接收输入消息,可以在activity运行的时候添加和删除(就像可以在其他activity中重用的"子ac ...
随机推荐
- Windows.Server.2003.R2 简体中文企业版 x86 x64(转)
两张盘,第二张是 R2安装盘. Windows.Server.2003.R2.With.Sp2 中文企业版[MSDN官方版本][32bit] Windows 2003.R2.With.Sp2 简体中文 ...
- NSTimer你真的会用了吗
http://www.cnblogs.com/smileEvday/archive/2012/12/21/NSTimer.html
- hdu4331 Image Recognition 就暴力啊。。啊。。
题意: 给一个1和0组成的正方形矩阵,求 四条边都由1构成的正方形的个数. 方法: 先统计矩阵中每一点,向四个方向,最多有多少个连续的1,这里用dp做也 与此同时,顺便求下 能向右下和 左上 两个方向 ...
- cocos2d-x 学习笔记一(概述)
1.游戏-可控制的动画-连续的静态图像 2.关键帧驱动游戏 3.事件驱动型游戏 4.cocos2d-x 1.0 opengl-es 1.0;cocos2d-x 2.x opengl-es 2.0;co ...
- 通过Jetty搭建一个简单的Servlet运行环境
最近在做一些简单的Servlet开发的时候,感觉每次调试的时候都要发布到tomcat上很麻烦,把程序共享给同事也很麻烦,需要帮他设置本地的tomcat环境. 在网上找了找其他的Servlet运行环境, ...
- 【十分不错】【离线+树状数组】【TOJ4105】【Lines Counting】
On the number axis, there are N lines. The two endpoints L and R of each line are integer. Give you ...
- JavaScript 【非IE DOM2级XML】
DOM2中的XML IE可以实现了对XML字符串或XML文件的读取,其他浏览器也各自实现了对XML处理功能.DOM2级在document.implementaion中引入了createDocument ...
- UVA 1617 Laptop
题意: 有n条长度为1的线段,确定他们的起点,必须是整数,使得第i条线段在[ri,di]之间.最后输出空隙的最小值 分析: 原始数据排序,排序的规则是先按照右端点排序,右端点相同的情况下,再按照左端点 ...
- 献给写作者的 Markdown 新手指南
「简书」作为一款「写作软件」在诞生之初就支持了 Markdown,Markdown 是一种「电子邮件」风格的「标记语言」,我们强烈推荐所有写作者学习和掌握该语言.为什么?可以参考: 『为什么作家应该用 ...
- C语言strcmp()函数:比较字符串(区分大小写)
头文件:#include <string.h> strcmp() 用来比较字符串(区分大小写),其原型为: int strcmp(const char *s1, const char *s ...