Android开发Fragment的使用学习
基本概念
Fragment是Android3.0(API11)提出的概念,support-v4库中也开发了一套Fragment API,最低兼容Android 1.6。所以在开发中要注意不要导错包
导入support-v4 compile 'com.android.support:support-v4:24.2.1
导入support-fragment com.android.support:support-fragment:24.2.1
- 一个Activity可以有多个Fragment
- Frament可以被多个Activity重用
- 它有自己的生命周期,可以接收监听事件
- 在运行Activity时可以被删除、替代。
生命周期

底部导航Demo
1、编写activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<fragment
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com."></fragment>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<ImageView
android:id="@+id/btn1"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/icon1"
android:layout_weight="1"
/>
<ImageView
android:id="@+id/btn2"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/icon2"
android:layout_weight="1"
/>
<ImageView
android:id="@+id/btn3"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/icon3"
android:layout_weight="1"
/>
<ImageView
android:id="@+id/btn4"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/icon4"
android:layout_weight="1"
/>
</LinearLayout>
</RelativeLayout>
2、一式4份准备fragment1_activity.xml的布局文件(背景和文件名改下)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/f1">
</LinearLayout>
3、Fragment_one.java文件一式4份,对于上一步的fragment布局
package com.example.fragmenttest;
import androidx.fragment.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
public class Fragment_one extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1_activty,container,false);
}
}
4、编写MainActivty.java
package com.example.fragmenttest;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView btn1;
ImageView btn2;
ImageView btn3;
ImageView btn4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
btn3 = findViewById(R.id.btn3);
btn4 = findViewById(R.id.btn4);
btn1.setOnClickListener(l);
btn2.setOnClickListener(l);
btn3.setOnClickListener(l);
btn4.setOnClickListener(l);
}
View.OnClickListener l = new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment f = null;
switch (v.getId()){
case R.id.btn1:
f = new Fragment_one();
break;
case R.id.btn2:
f = new Fragment_two();
break;
case R.id.btn3:
f = new Fragment_three();
break;
case R.id.btn4:
f = new Fragment_four();
break;
default:
break;
}
ft.replace(R.id.fragment,f);
ft.commit();
}
};
}
效果:

Android开发Fragment的使用学习的更多相关文章
- Android开发技术周报183学习记录
Android开发技术周报183学习记录 教程 Android性能优化来龙去脉总结 记录 一.性能问题常见 内存泄漏.频繁GC.耗电问题.OOM问题. 二.导致性能问题的原因 1.人为在ui线程中做了 ...
- Android开发——Fragment的简单使用总结
前言: 之前搞项目的时候,就使用了这个Fragment,中间遇到了许多坑,把坑都解决了,现在写一篇较为简单的Fragment使用总结 Fragment的简单介绍: 简单来说,Fragment其实可以理 ...
- Android开发技术周报182学习记录
Android开发技术周报182学习记录 教程 App安全二三事 记录 为什么要安全 App的移动安全主要包括下面几种: 密钥破解,导致本地加密数据被盗取. 通信密钥破解,导致接口数据被盗取. 伪造接 ...
- Android开发技术周报176学习记录
Android开发技术周报176学习记录 教程 当 OkHttp 遇上 Http 2.0 http://fucknmb.com/2018/04/16/%E5%BD%93OkHttp%E9%81%87% ...
- Android开发——Fragment知识整理(二)
0. 前言 Android开发中的Fragment的应用非常广泛,在Android开发--Fragment知识整理(一)中简单介绍了关于Fragment的生命周期,常用API,回退栈的应用等知识.这 ...
- Android开发——Fragment知识整理(一)
0. 前言 Fragment,顾名思义是片段的意思,可以把Fragment当成Activity的一个组成部分,甚至Activity的界面可以完全有不同的Fragment组成.Fragment需要被嵌 ...
- (转).net开发者对android开发一周的学习体会
春节期间,相对比较闲,上班时也没什么事情做.利用这一周的时间,简单的学习了一下移动方面的开发.主要是针对android,其实我对IOS更感兴趣 (因为我用iphone),苦于暂时没有苹果电脑,只能把它 ...
- Android开发----RecyclerView视图的学习
RecyclerView RecyclerView是什么? RecyclerView是如今Android开发中最常用的控件,其相较于ListView和GridView的功能更为强大,优化了两者的各种不 ...
- Android开发 ---Fragment片段布局
前言 Fragment想必大家不陌生吧,在日常开发中,对于Fragment的使用也很频繁,现在主流的APP中,基本的架构也都是一个主页,然后每个Tab项用Fragment做布局,不同选项做切换,使用起 ...
随机推荐
- css3,transition,animation两种动画实现区别
我们为页面设置动画时,往往会用到transition还有animation以及transfrom属性或者用到js. 其实通常情况下,对于使用js我们更加倾向于使用css来设置动画. transfrom ...
- Android_存储之DataBase之Room
概述: Room是Google在AndroidX中提供的一个ORM(Object Relational Mapping,对象关系映射)库.它是在SQLite上提供的一个抽象层,可以使用SQLite的全 ...
- Spring IoC componet-scan 节点解析详解
前言 我们在了解 Spring 容器的扩展功能 (ApplicationContext) 之前,先介绍下 context:componet-scan 标签的解析过程,其作用很大是注解能生效的关键所在. ...
- SSI PAYLOAD
<pre><!--#exec cmd="ls" --></pre><pre><!--#echo var="DATE_ ...
- Xilinx ISE多功能移位寄存器仿真及Basys2实验板实验
移位寄存器实现Verilog代码: `timescale 1ns / 1ps module add( input clk, input reset, input [1:0] s, input dl, ...
- Maven快速入门(三)Maven的坐标和仓库
之前通过一个helloworld的例子来说一说如何创建maven项目以及maven项目的项目结构,然后讲maven如何编译运行项目.接下来介绍maven中几个比较重要的概念:坐标和仓库.Maven快速 ...
- 【Hadoop】hdfs文件上传流程图
- MySQL 高级—— Join 、索引 、优化
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一.Join 查询 1.SQL执行顺序(一般情况下) 1.1 手写顺序: SELECT DISTINCT ...
- 移动端border:1px问题解决方案
了解设备像素和css像素的因该知道,通常我们在写移动端时,是按照设计稿标注的像素除以设备的DPR来写真实的像素, 比如在iPhone6上,我们写的20px字体世界上在视觉效应上有20px; 所以当我们 ...
- (Java实现) 洛谷 P1012 拼数
题目描述 设有nn个正整数(n≤20)(n≤20),将它们联接成一排,组成一个最大的多位整数. 例如:n=3n=3时,3个整数13,312,343联接成的最大整数为:3433121334331213 ...