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 ...
随机推荐
- Android教程2020 - RecyclerView使用入门
本文介绍RecyclerView的使用入门.这里给出一种比较常见的使用方式. Android教程2020 - 系列总览 本文链接 想必读者朋友对列表的表现形式已经不再陌生.手机上有联系人列表,文件列表 ...
- 聊一聊 MySQL 中的数据编辑过程中涉及的两阶段提交
MySQL 数据库中的两阶段提交,不知道您知道不?这篇文章就简单的聊一聊 MySQL 数据库中的两阶段提交,两阶段提交发生在数据变更期间(更新.删除.新增等),两阶段提交过程中涉及到了 MySQL 数 ...
- 利用geojson实现模型轨迹运动
直接上代码 var viewer = new Cesium.Viewer('cesiumContainer'); //Set the random number seed for consistent ...
- CAD制图系列一之绘图、标注、修改、视图
笔记内容: 缩放.平移.键盘操作 绘图:直线.矩形 修改:删除.修剪.延时 标注:线型.对齐.半径.折弯.直径.角度 知识点 鼠标中键上下滚动 平移:先全部选中,然后点击中间的空格,随便移动 重点:空 ...
- pico g2 触摸板手柄射线检测---for unity
1.pico g2手柄射线检测UI,需要在canvas添加Graphic Raycaster脚本和Pvr_Ui Canvas脚本. 2.删除掉原有的maincamera,将Pvr_unitySDK下h ...
- SubList到底怎么转化为ArrayList?
SubList 大家好,今天 Tony 给大家讲个SubList转化的坑. 这个错误真的会被忽略,大家好好的看看,这个错误我们生产环境还真的遇到过. 集合类型相信大家都很熟悉,在 Java 中 Arr ...
- HDU_1176_DP
http://acm.hdu.edu.cn/showproblem.php?pid=1176 简单dp,转换后跟上一题数塔一样,注意每秒只能移动一格,还有在边缘的情况. #include<ios ...
- 【WPF学习】第四十二章 透明
WPF支持真正的透明效果.这意味着,如果在一个性质或元素上层叠另外几个形状或元素,并让所有这些形状和元素具有不同的透明度,就会看到所期望的效果.通过该特性能够创建透过上面的元素可以看到的的图像背景,这 ...
- Codeforces 961C Chessboard(将碎了的、染色乱了的棋盘碎片拼一起)
题目链接:点击打开链接 Magnus decided to play a classic chess game. Though what he saw in his locker shocked hi ...
- Java并发编程-扩展可回调的Future
前提 最近在看JUC线程池java.util.concurrent.ThreadPoolExecutor的源码实现,其中了解到java.util.concurrent.Future的实现原理.从目前j ...