基本概念

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的使用学习的更多相关文章

  1. Android开发技术周报183学习记录

    Android开发技术周报183学习记录 教程 Android性能优化来龙去脉总结 记录 一.性能问题常见 内存泄漏.频繁GC.耗电问题.OOM问题. 二.导致性能问题的原因 1.人为在ui线程中做了 ...

  2. Android开发——Fragment的简单使用总结

    前言: 之前搞项目的时候,就使用了这个Fragment,中间遇到了许多坑,把坑都解决了,现在写一篇较为简单的Fragment使用总结 Fragment的简单介绍: 简单来说,Fragment其实可以理 ...

  3. Android开发技术周报182学习记录

    Android开发技术周报182学习记录 教程 App安全二三事 记录 为什么要安全 App的移动安全主要包括下面几种: 密钥破解,导致本地加密数据被盗取. 通信密钥破解,导致接口数据被盗取. 伪造接 ...

  4. Android开发技术周报176学习记录

    Android开发技术周报176学习记录 教程 当 OkHttp 遇上 Http 2.0 http://fucknmb.com/2018/04/16/%E5%BD%93OkHttp%E9%81%87% ...

  5. Android开发——Fragment知识整理(二)

    0.  前言 Android开发中的Fragment的应用非常广泛,在Android开发--Fragment知识整理(一)中简单介绍了关于Fragment的生命周期,常用API,回退栈的应用等知识.这 ...

  6. Android开发——Fragment知识整理(一)

    0.  前言 Fragment,顾名思义是片段的意思,可以把Fragment当成Activity的一个组成部分,甚至Activity的界面可以完全有不同的Fragment组成.Fragment需要被嵌 ...

  7. (转).net开发者对android开发一周的学习体会

    春节期间,相对比较闲,上班时也没什么事情做.利用这一周的时间,简单的学习了一下移动方面的开发.主要是针对android,其实我对IOS更感兴趣 (因为我用iphone),苦于暂时没有苹果电脑,只能把它 ...

  8. Android开发----RecyclerView视图的学习

    RecyclerView RecyclerView是什么? RecyclerView是如今Android开发中最常用的控件,其相较于ListView和GridView的功能更为强大,优化了两者的各种不 ...

  9. Android开发 ---Fragment片段布局

    前言 Fragment想必大家不陌生吧,在日常开发中,对于Fragment的使用也很频繁,现在主流的APP中,基本的架构也都是一个主页,然后每个Tab项用Fragment做布局,不同选项做切换,使用起 ...

随机推荐

  1. Spring全家桶一一SpringBoot与Mybatis

    Spring全家桶系列一一SpringBoot与Mybatis结合 本文授权"Java知音"独家发布. Mybatis 是一个持久层ORM框架,负责Java与数据库数据交互,也可以 ...

  2. 来自AI的Tips——情景智能

    来自AI的Tips--情景智能   上一次我们介绍了华为快服务智慧平台是什么,今天我们来侃一侃平台最有代表性的一个流量入口--情景智能(AI Tips).   首先情景智能在哪呢?大家可以拿出自己的华 ...

  3. [工具-006] C#如何模拟发包登录

    最近接到一个任务,就是模拟某个贴吧的登录发帖功能,我的思路是通过IE浏览器的工具对登陆操作进行抓包,记录登录时候请求的URL,请求方式,请求正文等信息进行模拟的发包. 1.首先我们要到登陆页面,以摇篮 ...

  4. HEARTBLEED 漏洞复现

    漏洞分析的两篇文章 https://blog.csdn.net/javajiawei/article/details/82429886 https://xz.aliyun.com/t/1771 set ...

  5. Rocket - debug - Periphery

    https://mp.weixin.qq.com/s/uGxn-Xec0LkwdaSsCtQBvw 简单介绍Periphery的实现. 1. ExportDebugDMI/ExportDebugJTA ...

  6. Java并发编程 (七) J.U.C之AQS

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一. J.U.C之AQS-介绍 1.定义: AbstractQueuedSynchronizer简称AQ ...

  7. Java实现 蓝桥杯 算法训练 第五次作业:字符串排序

    试题 算法训练 第五次作业:字符串排序 问题描述 输入一个小写字符串,按从小到大的顺序输出. 输入格式 bcaed 输出格式 abcde 顶格输出,中间没有空格 样例输入 一个满足题目要求的输入范例. ...

  8. Java实现 蓝桥杯 算法训练 谁干的好事?

    试题 算法训练 谁干的好事? 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 ABCDE中num个人做了好事,truth个人说真话. A说:"我和X中有且只有一个做了好事& ...

  9. Java实现 蓝桥杯VIP 算法提高 理财计划

    算法提高 理财计划 时间限制:1.0s 内存限制:256.0MB 问题描述 银行近期推出了一款新的理财计划"重复计息储蓄".储户只需在每个月月初存入固定金额的现金,银行就会在每个月 ...

  10. Java实现第九届蓝桥杯等腰三角形

    等腰三角形 题目描述 本题目要求你在控制台输出一个由数字组成的等腰三角形. 具体的步骤是: 1. 先用1,2,3,...的自然数拼一个足够长的串 2. 用这个串填充三角形的三条边.从上方顶点开始,逆时 ...