android中Fragment的使用

时间:2023-02-07 00:01:41

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

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

android中Fragment的使用

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

android中Fragment的使用

这里我创建了三个最简单的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

执行的效果:

android中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&plus;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. 运动曲线提升CSS动画效果

    原文链接 译文\译者鞠大宝 先有UI动画,然后才会有好的UI动画.好的动画会让人惊叹“哇哦!”——因为页面看上去很流畅.很漂亮,最重要的是,自然,一点都不会让人觉得不和谐或者僵硬死板.如果你经常逛Dr ...

  2. C&num;同一位置切换显示两个Panel内容

    如果两个panel重合在一起,点击不同按钮切换显示不同的panel,需要xxx.BringToFront(); 1.首先让两个panel的visible都为false, 在加载页面load方法里可以让 ...

  3. requirejs实践二 加载其它JavaScript与运行

    上一篇中介绍了requirejs加载JavaScript文件,在这一篇中介绍加载JavaScript后执行代码 这次是test2.html文件, <!DOCTYPE html> <h ...

  4. studio中集成&period;so文件的两种方式

    .so文件作为NDK的开发包,如果不进行依赖使用将会报错所以,需要.so的一定要配置 注:如果没有引用so文件,可能会在程序执行的时候加载类库失败,有类似如下的DEBUG提示:    java.lan ...

  5. Mycat安装与使用

      1.下载:   https://github.com/MyCATApache/Mycat-download 具体下载哪个版本以发布为准,推荐1.4,1.5.   2.安装:   安全前,在Linu ...

  6. Leetcode11 Container With Most Water 解题思路 (Python)

    今天开始第一天记录刷题,本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 准备按tag刷,第一个tag是array: 这个是array的第一道题:11. Container With ...

  7. Java&period;lang&period;Character类

    Character将一个char基本数据类型封装在类中.这个类中只有一char类型的变量.Character是基于unicode码进行的Character所有的方法,都是围绕着这个char基本数据类型 ...

  8. Ubuntu:双(多)网卡绑定(bonding)配置

    step 0:安装网卡绑定的功能 apt-get install    ifenslave step 1:加载内核模块:编辑 /etc/modules,添加: bonding step 2:编辑网卡配 ...

  9. 二分 poj 3273

    题目链接:https://vjudge.net/problem/POJ-3273 把n个连续的数字划分成m个连续的部分,每个部分都有一个部分和(这个部分所有值加起来),现在要使划分里最大的那个部分和最 ...

  10. 【LSTM】Understanding-LSTMs

    http://colah.github.io/posts/2015-08-Understanding-LSTMs/