基本概念

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. 【SpringBoot】SpringBoot 配置这一篇文章就够了

    SpringBoot 配置文件 一.配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的: application.properties application.yml 配置文件的 ...

  2. 织梦DEDECMS网站如何生成xml格式网站地图

    这个很简单,按照我下边提供的方法进行操作就可以了,我一直在用 第一步,登陆DedeCMS后台:核心 - 频道模型- 自由列表管理,点击添加列表: 参数按照下边的填写: 自由列表标题:[XML地图] 列 ...

  3. LNMP PHP升级脚本

    升级PHP前,请确认你的网站程序是否支持升级到的PHP版本,防止升级到网站程序不兼容的PHP版本,具体可以去你使用的PHP程序的官网查询相关版本支持信息.v1.3及以后版本大部分情况下也可以进行降级操 ...

  4. mysql 赋权语句

    grant all privileges on phplampDB.* to phplamp@localhost identified by '1234';

  5. HttpSession之简易购物车

    创建一个简单的购物车模型,由三个 jsp 和两个 Servlet 组成: step1.jsp <%@ page language="java" contentType=&qu ...

  6. Jpa使用详解

    目录 ORM思想 1.ORM概述 2.为什么要使用ORM 3.常见的ORM框架 JPA简介 1.JPA概述 2.JPA的优势 3.JPA与hibernate的关系 JPA入门案例 1.搭建开发环境 常 ...

  7. AddressBook/AddressBookUI

    概述 在iOS中,有2个框架可以访问用户的通讯录.从iOS6开始,需要得到用户的授权才能访问通讯录,因此在使用之前,需要检查用户是否已经授权ABAddressBookGetAuthorizationS ...

  8. Redis 单节点百万级别数据 读取 性能测试.

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 这里先进行造数据,向redis中写入五百万条数据,具体方式有如下三种: 方法一:(Lua 脚本) vim ...

  9. Java实现 LeetCode 412 Fizz Buzz

    412. Fizz Buzz 写一个程序,输出从 1 到 n 数字的字符串表示. 如果 n 是3的倍数,输出"Fizz": 如果 n 是5的倍数,输出"Buzz" ...

  10. Java实现 蓝桥杯 历届真题 稍大的串

    串可以按照字典序进行比较.例如: abcd 小于 abdc 如果给定一个串,打乱组成它的字母,重新排列,可以得到许多不同的串,在这些不同的串中,有一个串刚好给定的串稍微大一些.科学地说:它是大于已知串 ...