android中的Fragment跟网页中的iframe很像,用于在界面上嵌入局部动态内容,我的描述可能不准确,只是我的理解吧

创建Fragment很简单,在Android Studio中是这么创建的:

简单使用的话,下面的两个勾都可以不用勾选:

这里我创建了三个最简单的Fragment,代码就不粘了,每个Fragment里面可以放上最简单的TextView,显示一些文字信息等

然后我创建一个主Activity,我在主Activity上放三个按钮,点击对应的按钮,实现动态加载之前创建的Fragment

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"> <LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@android:color/holo_orange_light"
android:gravity="center_vertical"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintWidth_percent="0.5"> <Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button1" /> <Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button2" /> <Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button3" /> <TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="30dp" />
</LinearLayout> <FrameLayout
android:id="@+id/rightFrame"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_light"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintWidth_percent="0.5"> </FrameLayout>
</android.support.constraint.ConstraintLayout>

这个界面最外层是一个ConstraintLayout布局,里面放了一个上下结构的LinearLayout用于放按钮,还放了一个FrameLayout,用于动态加载我们之前创建的Fragment。

LinearLayout和FrameLayout我设置成各占屏幕的一半显示

下面是Activity类的代码

MainActivity.java:

 package com.example.chenrui.app1;

 import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button button = findViewById(R.id.button1);
button.setOnClickListener(this); button = findViewById(R.id.button2);
button.setOnClickListener(this); button = findViewById(R.id.button3);
button.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button24:
repalceFragment(new Fragment1());
break;
case R.id.button25:
repalceFragment(new Fragment2());
break;
case R.id.button26:
repalceFragment(new Fragment3());
break;
}
} public void repalceFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.rightFrame,fragment);
transaction.addToBackStack(null);
transaction.commit();
}
}

上面的代码中,从第45到51行,我们把动态加载Fragment的代码放在一个方法中。点击对应的按钮,加载对应的Fragment。

第49行代码的意思是切换Fragment后会记住历史,按返回键会返回到上一个加载的Fragment

执行的效果:

在主Activity中,怎么操作Fragment中的内容呢,在主Activity中可以这么写:

Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.rightFrame);
TextView textView = fragment.getView().findViewById(R.id.textView2);
textView.setText("大家好");

上面的代码通过getSupportFragmentManager().findFragmentById(R.id.rightFrame)获取到Fragment对象

textView2是Fragment的控件

反过来,在Fragment中怎么操作主Activity中的内容呢,在Fragment中要这么写:

TextView textView = getActivity().findViewById(R.id.textView1);
textView.setText("Hello");

上面的代码通过getActivity()方法获取到主Activity

textView1是主Activity的控件

android中Fragment的使用的更多相关文章

  1. Android中Fragment和ViewPager那点事儿(仿微信APP)

    在之前的博文<Android中使用ViewPager实现屏幕页面切换和引导页效果实现>和<Android中Fragment的两种创建方式>以及<Android中Fragm ...

  2. Android中Fragment与Activity之间的交互(两种实现方式)

    (未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...

  3. Android 中Fragment使用

    Android 中Fragment使用 public class MainActivity extends Activity { public static String[] array = { &q ...

  4. Android中Fragment的两种创建方式

    fragment是Activity中用户界面的一个行为或者是一部分.你可以在一个单独的Activity上把多个Fragment组合成为一个多区域的UI,并且可以在多个Activity中再使用.你可以认 ...

  5. Android中fragment之间和Activity的传值、切换

    功能介绍:通过一个activity下方的三个按钮,分别是发送消息(sendButton).聊天记录(chatButton).常用语(commonButton).当单击按钮是,来切换上方的fragmen ...

  6. Android中Fragment生命周期和基本用法

    1.基本概念 1. Fragment是什么? Fragment是可以让你的app纵享丝滑的设计,如果你的app想在现在基础上性能大幅度提高,并且占用内存降低,同样的界面Activity占用内存比Fra ...

  7. Android中Fragment+ViewPager的配合使用

    官方推荐 ViewPager与Fragment一起使用,可以更加方便的管理每个Page的生命周期,这里有标准的适配器实现用于ViewPager和Fragment,涵盖最常见的用例.FragmentPa ...

  8. Android中Fragment的简单介绍

    Android是在Android 3.0 (API level 11)引入了Fragment的,中文翻译是片段或者成为碎片(个人理解),可以把Fragment当成Activity中的模块,这个模块有自 ...

  9. 关于Android中Fragment静态和动态加载的方法

    一.静态加载 1.首先创建一个layout布局fragment.xml,里面放要显示和操作的控件 2.创建一个layout布局main1.xml,用来实现页面的跳转(跳转为要实现静态加载的界面) 3. ...

随机推荐

  1. 在Delphi中DBGrid有一个MouseMove事件,当鼠标移动时怎么知道光标在哪个单元格上面

    procedure TForm1.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);var coords:TGr ...

  2. 对一个前端使用AngularJS后端使用ASP.NET Web API项目的理解(3)

    chsakell分享了一个前端使用AngularJS,后端使用ASP.NET Web API的项目. 源码: https://github.com/chsakell/spa-webapi-angula ...

  3. 解决iframe加载的内容有时显示有时不显示

    在ASP.NET MVC项目中遇到了这样的一个问题,假设父页面有一个iframe <iframe id=" width="100%" height="10 ...

  4. IIS7.5标识介绍

    应用程序池的标识是运行应用程序池的工作进程所使用的服务帐户名称.默认情况下,应用程序池以 Network Service 用户帐户运行,该帐户拥有低级别的用户权限.您可以将应用程序池配置为以 Wind ...

  5. WordPress主题开发实例:产品展示

    产品展示用到文章和缩略图功能 实现步骤: 一.创建分类 后台创建文章分类:产品中心 二.开启缩略图功能 在主题的functions.php中,添加一段代码,代码如下: add_theme_suppor ...

  6. java获取视频播第一帧

    FFMPEG 功能很强大,做视频必备的软件.大家可通过 http://ffmpeg.org/ 了解.Windows版本的软件,可通过 http://ffmpeg.zeranoe.com/builds/ ...

  7. 福尔摩斯基本演绎法第一季/全集Elementary迅雷下载

    本季Elementary Season 1 第一季(2012)看点:<福尔摩斯:基本演绎法>由CBS出品,根据<福尔摩斯>系列改编,地点从19世纪大英帝国国势鼎盛的时期搬到了2 ...

  8. 通过图形化工具来画shape

    前两天一个哥们分享了十分好用的图形化工具,这样我们就能实时的看到自己用代码写出来的shape是怎么样的了,更牛的是它可以让我们自己去设定值,最后生成代码,这样我们为什么要去自己写shape呢?如果一个 ...

  9. w​i​n​d​o​w​s​ ​s​e​r​v​e​r​ ​2​0​0​8​ ​r​2​ ​启​用​索​引(转)

    08r2的“windows search”服务默认是不安装的,要想启用索引执行下列步骤:        1.打开“服务器管理”——选中“角色”——右边选中“添加角色”——勾选“文件服务”.    2. ...

  10. 如何在CentOS 7.2上创建NFS的Share,然后让Client可以访问

    讲得详细清楚明白的好文. Setting Up an NFS Server and Client on CentOS 7.2 https://www.howtoforge.com/tutorial/s ...