Android中通过Fragment进行简单的页面切换
首先是activity中的布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"> <FrameLayout
android:id="@+id/fragment" android:layout_width="395dp"
android:layout_height="509dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.333"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"> </FrameLayout> <Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginBottom="12dp"
android:text="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" /> <Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="64dp"
android:layout_marginBottom="17dp"
android:text="2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
创建两个Fragment子类
这里以一个为例
package com.example.fragment; import android.os.Bundle; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment; import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class f1 extends Fragment { @Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.f1_fragment2, container, false);
} }
其布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/f1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="f1">
<TextView
android:id="@+id/textView4"
android:layout_width="182dp"
android:layout_height="85dp"
android:layout_marginTop="165dp"
android:text="我是1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
最后也是最重要的,Mainactivity内容:
package com.example.fragment; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class MainActivity extends AppCompatActivity {
private Button b1=null;
private Button b2=null;
private FragmentManager fm=null ;
private FragmentTransaction transaction =null ; private f1 f1;
private f2 f2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.b1);
b2=(Button)findViewById(R.id.b2);
fm = getSupportFragmentManager(); setDefaultFragment();
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
transaction = fm.beginTransaction();
f1=new f1();
transaction.replace(R.id.fragment,f1);
transaction.commit();
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { transaction = fm.beginTransaction();
f2=new f2();
transaction.replace(R.id.fragment,f2);
transaction.commit();
}
});
} private void setDefaultFragment()
{
transaction = fm.beginTransaction();
f1=new f1();
transaction.replace(R.id.fragment,f1);
transaction.commit();
} }
注意:每个FragmentTransaction只能提交一次,因此在每次提交前都要重新为transaction赋予一个新对象;
还有关于“fm = getSupportFragmentManager();”处使用“getSupportFragmentManager();”,而不使用“fm = getFragmentManager();”的原因请参照这篇帖子https://blog.csdn.net/qq_28484355/article/details/67824228
效果:

点击“2”后:

Android中通过Fragment进行简单的页面切换的更多相关文章
- android中viewPager+fragment实现的屏幕左右切换(进阶篇)
Fragment支持在不同的Activity中使用并且可以处理自己的输入事件以及生命周期方法等.可以看做是一个子Activity. 先看一下布局: 1 <LinearLayout xmlns:a ...
- Android中ViewPager+Fragment取消(禁止)预加载延迟加载(懒加载)问题解决方案
转载请注明出处:http://blog.csdn.net/linglongxin24/article/details/53205878本文出自[DylanAndroid的博客] Android中Vie ...
- Android笔记(十九) Android中的Fragment
通常我们使用Activity来展示界面,但是在手机上界面可能显示的很好看,但在平板上,因为平板的屏幕非常大,手机的界面放在平板上可能会出现控件被拉长.控件之间间距变大等问题.为了更好的体验效果,在Ac ...
- Android 中关于Fragment嵌套Fragment的问题
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/5802146.html 问题描述: 在项目中Activity A中嵌套Fragment B,Fragment ...
- Android中GPS定位的简单应用
在Android中通过GPS获得当前位置,首先要获得一个LocationManager实例,通过该实例的getLastKnownLocation()方法获得第一个的位置,该方法的说明如下: void ...
- ViewPager (下)-- 利用 Fragment 实现美丽的 页面切换
之前用的ViewPager适用于简单的广告切换,但实现页面间的切换最好是用官方推荐的Fragment来处理. 本人力争做到最简单.最有用,是想以后用到的时候能够方便的拿过来复制就能够了. 效果图: w ...
- Android中的Fragment页面切换和selector选择器
效果如图: 提示:下面是用的整个的图片 下面看代码: //--------------------这是主页面布局文件----------------------- <?xml version=& ...
- Android中ViewPgae中的Fragment如何确认当前页面可见的问题
由于在ViewPage中PageAdapter来管理所有的Fragment.在加载一个Fragment的时候,会自动缓存左右几个(默认是一个)页面,此时也会调用到正常的生命周期函数,onCreate, ...
- 【IOS】ios中NSUserDefault与android中的SharedPreference用法简单对比
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3405308.html 有Android开发经验的朋友对Shar ...
随机推荐
- Linux 安装tomcat及tomcat自带远程部署项目与管理
准备: 1.Linux系统 2.已经安装好jdk 开始: 选择要安装的tomcat版本:https://archive.apache.org/dist/tomcat/ 我这里使用的是tomcat 8. ...
- Shell之信号捕获
前言 当我们在运行某一段代码的时候,希望有类似事物一样的操作,要么成功,要么失败:一般的shell脚本都是自上而下,从左之后运行,碰到异常信号就会出错,从而终止脚本的运行,这个时候脚本可能运行到某一处 ...
- day05【数组】
day05[数组] 1.数组 概念:是一种容器,可以同时存放多个数据. 特点: 数组是一种引用数据类型 数组当中的多个数据,类型必须统一 数组的长度在程序的运行期间不可改变 初始化:在内存当中创建一个 ...
- 【Java并发基础】利用面向对象的思想写好并发程序
前言 下面简单总结学习Java并发的笔记,关于如何利用面向对象思想写好并发程序的建议.面向对象的思想和并发编程属于两个领域,但是在Java中这两个领域却可以融合到一起.在Java语言中,面向对象编程的 ...
- DRF框架之Serializer序列化器的反序列化操作
昨天,我们完成了Serializer序列化器的反序列化操作,那么今天我们就来学习Serializer序列化器的最后一点知识,反序列化操作. 首先,我们定要明确什么是反序列化操作? 反序列化操作:JOS ...
- CAD制图系列之椭圆画法标注
今天我将做一个极轴是92,150的椭圆画法和标注方法 1.打开2014版本CAD制图,快捷键EL,回车: 2.自己随便定一个点 3.输入第一个值,也就是短轴--横轴(输入实际长度,不需要除以二)并且鼠 ...
- js的三种输出语句,以及html的运行循序
js最常见的三种输出语句 1.console.log()这个语句是在浏览器控制台输出的.进入网页点击f12可查看 2.alert()弹出一个对话框, 3.document.write这个语句是在页面输 ...
- 编写一个函数,输入n为偶数时,调用方法求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n
需求:编写一个函数,输入n为偶数时,调用方法求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n package com.Summer_0511.cn; impo ...
- 表达式属性(C#6.0和C#7.0
从C#6开始,只读属性可简写为表达式属性.它使用双箭头替换了花括号,get访问器和return关键字. 例如: decimal CurrentPrice,sharedOwned; public dec ...
- 2020牛客寒假算法基础集训营4 E:最小表达式
E:最小表达式 考察点 : 贪心,高精度 坑点 : 高精度一定不要写错,一定一定不要写错 剩下的就是细节问题 侃侃 : 1.字符串长度达到 5e5,如果要涉及到加法,乘法,普通的肯定会爆 long l ...