Android-Fragment(基本知识,静态加载,动态加载)
来源:互联网 发布:二叉树实现c语言实现 编辑:程序博客网 时间:2024/05/07 02:43
1.回顾
上篇了解了和学习了 Andorid 的webView 的使用
2.重点
(1)Fragment基本知识
(2)Fragment 静态加载
(3)Fragment 动态加载
(4)Fragment的生命周期
(5)Fragment与Activity 之间相互通信(传值)
3.Fragment基本知识
(1)可以作为Activity一部分
(2)可以在Activity 出现多个Fragment
(3)可以在多个Activity 中出现一个Fragment
(4)Fragment在Activity 运行过程中,可以添加,移除和替换
(5)Fragment可以响应自己的输入事件
(6)Fragment的独立的生命周期,受宿主Acitivity的生命周期影响
(7)独立的组件控制
(8)独立的用户输入响应
4.Fragment静态 加载
4.1 新建 layout 布局文件 fragmen.xml
包括一个按钮和一个 textview
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> <Button android:id="@+id/btn_frag" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="改变" /></LinearLayout>
4.2 新建 MyFragment
继承自 Fragment ,实现 onCreateView() 方法,其中的 View对象 中需要的布局文件为 ,上面新建的fragment.xml 布局文件
package com.example.fragment;import com.example.studydemo.R;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;public class MyFragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// layout布局文件 转成view 对象/** * resource 布局文件 * root 加载layout 的父 ViewGroup * attachToRoot :false 不反悔 */View view=inflater.inflate(R.layout.fragment, container, false);TextView textView1=(TextView) view.findViewById(R.id.textView1);textView1.setText("静态加载Fragment");return view;}}
4.3 新建Activity
布局文件中新添加 fragment 标签,引入刚刚新建的 MyFragment 实现类 ; 一定要给 fragment 添加id
<fragment android:id="@+id/fragment_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:name="com.example.fragment.MyFragment" > </fragment>
4.4 测试
在 新建的Activity 类中 ,实例话 fragment中的 控件 按钮和 textview ;
btn_frag=(Button) findViewById(R.id.btn_frag);tv1=(TextView) findViewById(R.id.textView1);btn_frag.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//点击事件tv1.setText("我是Fragment2Activity");}});
4.5 运行测试
5. Fragment 动态加载
5.1 新建 Activity
在布局文件中 添加一个 按钮和 一个 布局文件
<Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/button3" android:text="动态加载Fragment" /> <LinearLayout android:id="@+id/fragme" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="30dp" android:layout_marginTop="42dp" android:orientation="vertical" > </LinearLayout>
5.2 事件
初始化按钮,和在按钮点击事件里添加,动态加载代码:
例如:
(1)MyFragment 和静态加载一样 实现 Fragment ,
(2)transaction.add()方法中 的 R.id.fragme 是 上面 lineatLayout 的 id
(3)最后一定 commit
MyFragment2 fragment2=new MyFragment2(); FragmentManager fragmentManager= getFragmentManager(); FragmentTransaction transaction=fragmentManager.beginTransaction(); transaction.add(R.id.fragme, fragment2); //回退 transaction.addToBackStack(null); transaction.commit();
6.总结
动态加载的时候,FragmentTransaction 有 多个操作 Fragment操作方法,比如 add() , replace() ,remove()等
下面将学习 Fragment的 生命周期和 与 Activity 相互通信(传参)
- Android-Fragment(基本知识,静态加载,动态加载)
- Android-动态加载和静态加载Fragment
- Fragment 动态加载 / 静态加载
- android 静态加载Fragment和动态加载Fragment
- Fragment生命周期、静态加载、动态加载
- FragMent的静态加载和动态加载
- Android笔记(8)---Fragment的使用(静态加载,动态加载)
- 喝酒不骑马的Android自学日记(12)-Fragment动态加载和静态加载
- Android导入design.v4包,Fragment生命周期,静态/动态加载
- Android之Fragment静态加载
- Android之Fragment静态加载
- Android笔记 fragment入门 静态加载fragment
- 一起Talk Android吧(第四十三回:Android中的Fragment八-静态加载VS动态加载)
- Fragment 的两种加载方式 (静态加载,动态加载)
- fragment的静态与动态加载
- Android viewpager+fragment实现动态加载碎片(fragment重复使用)
- Android——Fragment(碎片)的静态加载方式....
- android Fragment静态加载使用详解
- 一道递归算法--String[] 里面的字符无重复的排序
- sRGB
- 1.4、安装Docker Compose
- 为一个viewcontroll添加web view的方法
- ubuntu里面设置颜色
- Android-Fragment(基本知识,静态加载,动态加载)
- Util:Logcat统一管理类
- sRGB 和 Adobe RGB 有什么区别?
- Linux Ubuntu server安装Java JDK1.7_55
- 【NOIP2006TG/codevs1154】 能量项链 解题报告
- rust里使用thread_local!
- html中select标签刷新后不回到默认值而是保持之前选择值
- 使用apache JMeter进行web服务器的性能/压力/负载测试
- hibernate 一些查询操作