Fragment

1)Fragment的简单用法

2)动态添加Fragment

3)在Fragment中模拟返回栈

4)Fragment和活动之间通信

第四章 Fragment

Fragment是一种可以嵌入在活动当中的UI片段,它能让程序更加合理和充分的利用大屏幕的空间,比如横屏和平板。

4.2 Fragment的使用方式

创建一个平板模拟器

4.2.1 Fragment的简单用法

两个Fragment平分活动,新建一个FragmentTest项目。

1.左侧fragment与右侧fragment的布局创建

新建一个left_fragment.xml,放置一个按钮并居中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Button"/> </LinearLayout>

新建一个right_frgment.xml,放置一个TextView用于显示文本

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20dp"
android:text="This is right fragment"/>
</LinearLayout>

2.新建左fragment和右fragment的类,继承自Fragment

(推荐使用android.support.v4.app.Fragment,能让Fragment在所有Android系统版本中保持功能一致性)

新建一个LeftFragment类,继承自Fragment

重写了onCreatView()方法,通过inflate()方法将刚才定义的left_fragment布局动态加载进来

public class LeftFragment extends Fragment {

    @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left_fragment, container, false);
return view;
}
}

同理,建立一个RightFragment类,继承自Fragment

public class RightFragment extends Fragment{

    @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.right_fragment, container, false);
return view;
}
}

3.修改activity_main.xml文件中的代码

元素设置为水平放置,用<fragment>标签在布局中添加fragment,通过android:name属性来显示指明添加的碎片类名

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"> <fragment
android:id="@+id/left_fragment"
android:name="com.example.song.fragmenttest.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/> <fragment
android:id="@+id/right_fragment"
android:name="com.example.song.fragmenttest.RightFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/> </LinearLayout>

运行程序:

两个碎片平分了整个活动

4.2.2 动态添加碎片

1.新建another_right_fragment.xml

和right_fragment.xml中额代码基本相同,改了背景色和颜色

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#ffff00"
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="This is another right Fragment" /> </LinearLayout>

2.新建AnotherRightFragment作为另一右侧碎片

public class AnotherRightFragment extends Fragment {

    @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.another_right_fragment, container, false);
return view;
}
}

3.动态的加入activity_main.xml中

将右侧fragment替换为了<FramLayout>(Android中最简单的布局,所有控件会默认摆放在布局的左上角),即将another_right_fragment以FrameLayout的形式放在右侧

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"> <fragment
android:id="@+id/left_fragment"
android:name="com.example.song.fragmenttest.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/> <FrameLayout
android:id="@+id/right_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"> </FrameLayout>
</LinearLayout>

4.修改MainActivity中的代码

当点击左侧fragment中的按钮时,会调用replaceFragment()方法将右侧碎片替换成AnotherRightFragment

public class MainActivity extends AppCompatActivity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
replaceFragment(new RightFragment());
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
replaceFragment(new AnotherRightFragment());
}
});
} private void replaceFragment(Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction =
fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout, fragment);
transaction.commit(); }

}

动态添加碎片主要分5步:

(1)创建待添加的fragment实例

(2)获取FragmentManager,在活动中可以直接通过调用getSupportFragmentManager()方法得到

(3)开启一个事务,通过调用beginTransaction()方法来开启

(4)向容器内添加或替换fragment,一般使用replace()方法,需要传入容器的id和待添加的fragment实例

(5)提交事务,通过commit()方法完成

运行程序:

4.2.3 在fragment中模拟返回栈

按下back键返回上一个Fragment

在FragmentTransacition提供一个addToBackStack()方法即可,用于将一个实务添加到返回栈中,传入null即可。

    private void replaceFragment(Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout, fragment);
transaction.addToBackStack(null);
transaction.commit(); }

4.2.4 fragment和活动之间的通信

1.活动调用fragment里的方法

RightFragment rightFragment = (RightFragment) getSupportFragmentManager().
findFragmentById(R.id.right_fragment);

可以得到相应fragment的实例然后调用其中的方法。

2.fragment调用活动中的方法

MainActivity activity = (MainActivity) getActivity();

可以得到相应活动的实例然后调用其中的方法。

3.当fragment需要使用Context对象时,亦可以使用getActivity()方法,因为获取到的活动本身为一个Context对象。

<Android基础>(四) Fragment Part 1的更多相关文章

  1. <Android基础> (四) Fragment Part 2

    4.3 Fragment的生命周期 4.3.1 Fragment的状态和回调 1.运行状态 当一个Fragment是可见的,并且它关联的活动正处于运行状态是,该Fragment也处于运行状态 2.暂停 ...

  2. Android基础——使用Fragment适应不同屏幕和分辨率

    最近事情很忙,一个新项目赶着出来,但是很多功能都要重新做,一直在编写代码.Debug.今天因为一个新程序要使用Fragment来做,虽然以前也使用过Fragment,不过没有仔细研究,今天顺道写篇文章 ...

  3. 【Android基础】Fragment 详解之Fragment生命周期

    上一篇文章简单介绍了一下Fragment,这一篇文章会详细的说一下Fragment的生命周期和创建一个用户界面. Fragment的主要功能就是创建一个View,并且有一个生命周期来管理这个View的 ...

  4. 【Android基础】Fragment 详解之Fragment介绍

    Fragment在Android 3.0( API 11)引入,是为了支持在大屏上显示更加动态.灵活的UI,比如在平板和电视上.Fragment可以看作是嵌套的Activity,类似ActivityG ...

  5. Android基础知识 -- Fragment

    Fragment是android3.0后提供的API(所以android:minSdkVersion="11"以上版本),主要针对平板UI.有自己的生命周期,但是必须依附在Acti ...

  6. 安卓Android基础四天

    网页源码查看器 HttpURLConnection:用于发送和接受数据 ScrollView只能由一个孩子 消息机制的写法(***) anr Application not response 应用无响 ...

  7. android基础学习-Fragment和eclipse快捷键

    使用Fragment的原因 1. Activity间的切换不流畅 2. 模块化Activity,方便做局部动画(有时为了到达这一点要把多个布局放到一个activity里面,现在可以用多Fragment ...

  8. Android基础——Fragment与Activity交互

    今天继续讲解Fragment组件的特性,主要是跟Activity的交互和生命周期的关系,我们前面已经说过Fragment是依赖于Activity的,而且生命周期也跟Activity绑定一起.下面我们看 ...

  9. Android基础——Fragment控制切换多个页面

    今天接着上一篇文章,讲解一下Fragment的控制,主要是切换View和页面替换等操作.还有就是如何获取Fragment的管理对象,以及与Activity的通信方式. (PS:新建的QQ群,有兴趣可以 ...

随机推荐

  1. 瓦片切图工具gdal2tiles.py改写为纯c++版本(二)

    python这么火,C++/C#的程序员都生存不下去了,为啥还要干把python转写成c++的这种反动的事? 项目需要呗... gdal2tiles.py文件中有两个类是计算全球墨卡托与WGS84两种 ...

  2. selenium-弹窗操作(八)

    本次以笔者公告栏的 打赏 弹窗为例 对弹窗中的一些操作进行封装后,在测试中使用 作用:减少对弹窗反复操作时进行定位的麻烦,以后使用中都直接调用即可达到目的 # coding=utf-8 from se ...

  3. 时序数据库InfluxDB安装及使用

    时序数据库InfluxDB安装及使用 1 安装配置 安装 wget https://dl.influxdata.com/influxdb/releases/influxdb-1.3.1.x86_64. ...

  4. c/c++ 多线程 等待一次性事件 future概念

    多线程 等待一次性事件 future概念 背景:有时候,一个线程只等待另一个线程一次,而且需要它等待的线程的返回值. 案例:滴滴叫车时,点完了叫车按钮后,叫车的后台线程就启动了,去通知周围的出租车.这 ...

  5. 3星|《给产品经理讲技术》:APP开发技术介绍,没有技术背景的话恐怕只能看懂书中的比喻和结论

    基本是APP开发涉及到的相关技术的入门级介绍.涉及到的知识点与技术细节比较多,不少技术相关的内容并没有像标题暗示的那样没有技术背景也可以看懂,而是涉及到许多专业的术语.原理.也有一些内容是用比喻的方法 ...

  6. vue 组件开发、vue自动化工具、axios使用与router的使用(3)

    一. 组件化开发 1.1 组件[component] 在网页中实现一个功能,需要使用html定义功能的内容结构,使用css声明功能的外观样式,还要使用js定义功能的特效,因此就产生了一个功能先关的代码 ...

  7. 【Python 07】汇率兑换1.0-2(基本元素)

    1.Python基本元素 (1)缩进:表示代码层次关系(Python中表示程序框架唯一手段) 1个tab或者4个空格 (2)注释:开发者加入的说明信息,不被执行.一个代码块一个注释. # 单行注释(一 ...

  8. Python开发【内置模块篇】collections

    namedtuple namedtuple是一个函数,它用来创建一个自定义的tuple对象,并且规定了tuple元素的个数,并可以用属性而不是索引来引用tuple的某个元素. 这样一来,我们用name ...

  9. vue li click

    <ul>      <li @click="mechanisms(1)">AAAAA</li>      <li @click=" ...

  10. Error response from daemon: conflict: unable to remove repository reference 解决方案

    由于前一章演示用的镜像没什么用准备删除 docker image rm hello-world:latest Error response from daemon: conflict: unable ...