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 ...
 
随机推荐
- C语言—期末小黄衫获奖感言
			
小黄衫获奖感言 一,感谢环节 非常感谢邹欣,周筠老师给提供的小黄衫,我非常荣幸的能够获得这个奖项,我感到无比自豪.感谢两位老师对教学事业的大力支持,对我们学生的亲切关怀.同时感谢我的C语言老师彭琛(琛 ...
 - php编译完php.ini加载问题-Loaded Configuration File (none)
			
编译安装php7时指定了--with-config-file-path=/usr/local/php7/etc,修改了 php.ini 的配置后重启,但就是不生效. 出现Loaded Configur ...
 - sqli_labs学习笔记(一)Less-54~Less-65
			
续上,开门见山 暴库: http://43.247.91.228:84/Less-54/?id=-1' union select 1,2,database() --+ challenges 爆表: h ...
 - CSS-16-margin值重叠问题
			
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
 - linux--->linux 各个文件夹及含义
			
1./bin 是binary的缩写 存放linux常用命令 2./lib 该目录用来存放系统动态链接共享库,几乎所有的应用程序都会用到该目录下的共享库. 3./dev 该目录包含了Linux系统中使用 ...
 - Warshall算法求传递闭包及具体实现
			
传递闭包 在数学中,在集合 X 上的二元关系 R 的传递闭包是包含 R 的 X 上的最小的传递关系. 例如,如果 X 是(生或死)人的集合而 R 是关系“为父子”,则 R 的传递闭包是关系“x 是 y ...
 - Kubernetes学习(二)
			
二 POD生命周期 initC作用说明 initC举例说明 init-pod.yaml apiVersion: v1kind: Podmetadata: name: myapp-pod labels: ...
 - Distance Dependent Infinite Latent Feature Model 阅读笔记1
			
阅读文献:Distance Dependent Infinite Latent Feature Model 作者:Samuel J.Gershman ,Peter I.Frazier ,and Dav ...
 - Docker(三):利用Kubernetes实现容器的弹性伸缩
			
一.前言 前两章有的介绍docker与Kubernetes.docker是项目运行的容器,Kubernetes则是随着微服务架构的演变docker容器增多而进行其编排的重要工具.Kubernetes不 ...
 - Linux 误删catlina.out导致磁盘空间爆满,无法查询到大文件解决办法
			
大概是前俩天吧,发现公司的网站不定时的出现接口调不通的情况,便让手下小弟去服务器上查看一下,小弟告我磁盘空间满了,于是我让他处理一下.结果没想到他直接把 catlina.out 给干掉了.后果可想而知 ...