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做布局,不同选项做切换,使用起 ...
随机推荐
- springmvc拦截器和概念,配置!!!
用于拦截请求,过滤后再拦截 实现HandlerInterceptor接口 配置拦截器 package cn.zys.lanjieqi; import javax.servlet.http.HttpSe ...
- js 遍历删除数组
$(function(){ var aa = [1,1,2,3,3,4,4,5]; alert(aa); for (var i = aa.length-1;i >= 0 ;i--) { if ( ...
- 【SocketIoClientDotNet】Nuget包安装问题
问题: Nuget安装[SocketIoClientDotNet]失败 错误信息: Operation failed Expected 1 export(s) with contract name & ...
- 微信小程序订阅
微信小程序订阅 摘要 1.基于promise封装微信小程序订阅 2.解决由于微信基础库版本低下的兼容 3.解决“总是保持以上选择,不再询问”的取消状态 主要运用API: requestSubscrib ...
- Mybatis执行器源码手记
今天将Mybatis的执行器部分做一下简单手记. 一.java原生JDBC 众所周知,Mybatis是一个半自动化ORM框架.其实说白了,就是将java的rt.jar的JDBC操作进行了适度的封装.所 ...
- ES6-Promise承诺
1.扩展:ajax 1.1 代码如下 $.ajax({ url:'地址', type:'get/post', data:{}, dataType:'json/jsonp', success:funct ...
- ES6-解析赋值
1.注意事项 A.左右两边结构必须一样 B.右边必须是个东西 C.声明和赋值不能分开(必须在一句话里完成) 2.代码如下 <!DOCTYPE html> <html lang=&qu ...
- Java实现 LeetCode 292 Nim游戏
292. Nim 游戏 你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解 ...
- Java实现 洛谷 P1000 超级玛丽游戏
public class Main { public static void main(String[] args){ System.out.println(" ********" ...
- Java实现 蓝桥杯 历届试题 剪格子
问题描述 如下图所示,3 x 3 的格子中填写了一些整数. +--*--+--+ |10* 1|52| +--****--+ |20|30* 1| *******--+ | 1| 2| 3| +--+ ...