首页 技术 正文
技术 2022年11月13日
0 收藏 768 点赞 5,003 浏览 3769 个字

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的控件

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289