Fragment是什么,怎么用?
1.什么是Fragment?
fragment它自己的中文意思:碎片;
一个可以将activity拆分成几个完全独立封装的可重用的组件,每个组件有自己的生命周期和ui布局。
2.用fragment能解决什么问题?
说明:总的来说,Fragment和Activity的生命周期类似。需要注意的是,它相比于Activity,多了onAttach(), onDetch(), onCreateView()和onDestroyView()这几个回调函数;但是,却少了onRestart()。
Fragment的生命周期非常复杂,分为以下几种情况:
- 如果是通过XML中的<fragment/>标签实例化的,那么第一个收到的回调将是onInflate
- 如果setRetainInstance(true),那么当Activity重建时,Fragment的onDestroy以及Activity重建后Fragment的onCreate回调不会被调用.(无论是否将其添加到了返回栈)
- 如果当前显示的是Fragment A,然后执行FragmentTransaction.replace(),那么Fragment A会执行onPause()->onStop()->onDestroyView()->onDestroy()->onDetach(),如果执行FragmentTransaction.replace().addToBackStack(),那么Fragment A会执行onPause()->onStop()->onDestroyView()
- FragmentTransaction.hide(),将不会导致onPause(),而是会触发onHiddenChanged()
- FragmentTransaction.detach(),会导致onPause()->onStop()->onDestroyView(),注意:onDestroy()和onDetach()不会调用
3.fragment静态加载方法
fragment静态加载所用的布局,android:name属性中是MyFragment.java的全名,android:id中是Fragment的唯一标识(这个必须得加,否则报错,也可用android:tag属性来作唯一标识)。
- <fragment
- android:name="com.example.myfragment.MyFragment"
- android:id="@+id/myfragment_1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
4.fragment动态加载方法
1.创建一个类继承Fragment,复写onCreateView方法。
例如:
public class AnotherRightFragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){ //传进fragment布局文件创建一个view对象 View view =inflater.inflate(R.layout_another_right_fragment,container,flase);return view; }
}
2.在MainActivity中创建待添加的fragment实例。
AnotherRightFragment fragment = new AnotherRightFragment();
3.在Activity中通过调用个体FragmentManager()方法获取到FragmentManager。
FragmentManager fragmentManager = getFragmentManager();
4.开启一个事物,通过调用beginTransaction()方法开启。
FragmentTransaction transaction = fragmentManager.beginTransaction();
5.向容器内加入Fragment,一般使用replace()方法实现,需要传入容器的id和待添加的碎片实例。
transaction.replace(R.id.right_layout,fragment);
6.提交事务,调用commit()方法来完成。
transaction.commit();
5.viewpager+fragment实现页卡滑动切换
布局文件activity_main.xml如下:
<?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"><LinearLayout
android:id="@+id/linearLayout1"android:layout_width="fill_parent"android:layout_height="@dimen/top_tab_height"android:background="@color/main_top_color" ><TextView
android:id="@+id/picture_text"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1.0"android:gravity="center"android:text="@string/picture"android:textStyle="bold"android:textColor="@color/main_top_tab_color"android:textSize="@dimen/main_top_tab_text_size" /><TextView
android:id="@+id/movie_text"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1.0"android:gravity="center"android:text="@string/movie"android:textStyle="bold"android:textColor="@color/main_top_tab_color"android:textSize="@dimen/main_top_tab_text_size" /><TextView
android:id="@+id/music_text"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1.0"android:gravity="center"android:text="@string/music"android:textStyle="bold"android:textColor="@color/main_top_tab_color"android:textSize="@dimen/main_top_tab_text_size" /></LinearLayout><LinearLayout
android:layout_width="match_parent"android:layout_height="@dimen/main_line_height"android:layout_gravity="bottom"android:orientation="vertical"android:background="@color/main_top_color"><ImageView
android:id="@+id/cursor"android:layout_width="@dimen/main_matrix_width"android:layout_height="@dimen/main_line_height"android:scaleType="matrix"android:src="@color/matrix_color" /></LinearLayout><View
android:layout_width="fill_parent"android:layout_height="0.5dp"android:background="@color/main_top_color"/><android.support.v4.view.ViewPager
android:id="@+id/vPager"android:layout_width="fill_parent"android:layout_height="0dp"android:layout_gravity="center"android:layout_weight="1.0"android:background="@color/white"android:flipInterval="30"android:persistentDrawingCache="animation" />
</LinearLayout>
Fragment是什么,怎么用?相关推荐
- Fragment之间传递数据的方式
1.直接调用另一个Fragent对象的方法,两个Fragment之间高度耦合 2.采取接口回调的方式进行数据传递.即在一个fragment中创建一接口以及接口对应的set方法,在另一个fagment中 ...
- Android Fragment 调用宿主Activity 里面的方法
方法 1 : 直接在Fragment 写 MainActivity main = (MainActivity) getActivity();main.xx; // xx 是 MainActivity ...
- java.lang.IllegalArgumentException: No view found for id 0x7f07005f (xx) for for fragment xxFragment
问题的原因的是自己修改id 的时候弄错了修改下id 就好了 我的是用activity 加载fragment 使用的地方如下 ,确定下面的id 是否在布局中 transaction.add(R.id.c ...
- overridePendingTransition 方法在Fragment 中使用
overridePendingTransition 方法在直接卸载Fragment 中无法识别 这个使用可以在前面添加getActivity 即可使用了 getActivity().override ...
- Fragment 使用 replace 的方式实现切换 以及切换的时候Fragment 生命周期
这个主要代码在activity里面 如下 public class ReplaceActivity extends AppCompatActivity implements View.OnClickL ...
- Fragment 使用 show 和 hide 的方式实现切换 以及切换的时候Fragment 生命周期
实现的效果如下图 主要的代码在activity 这里贴出来了 public class ShowActvity extends AppCompatActivity implements View.On ...
- ViewPager与Fragment结合使用,以及切换的时候Fragment 的生命周期
下面要做的效果图下图 首先我们创建一个适配器如下 public class FraPagerAdapter extends FragmentPagerAdapter {private List< ...
- Activity 数据传递给Fragment
下面说的宿主Activity 里面的Fragment 加入现在Fragment 里面 想使用Activity 里面的城市id 方法 1 使用set值 在Fragment 里面写一个set方法 在act ...
- Fragment 之间传递数据
关于Fragment 之间的跳转这里就不再说了 有兴趣的可以看我的这边博客 Fragment 跳转 ,,,点击查看把 Fragment 跳转首先先跳转到宿主的Activity 上 这里以在同一个Act ...
- Tablayout 多个界面使用一个fragment 的实例
这个主要还是adapter 里面 添加list 就行了 这里直接上代码吧 ,我刚写的demo 看的时候看adapter 就行了 布局代码: <?xml version="1.0&quo ...
最新文章
- softmax函数为什么叫softmax?
- 《http权威指南》阅读笔记(十)
- 双11奇迹背后的大数据平台,不喧哗,自有声!
- go 语言链接服务器上的mysql数据库
- 一个程序员的一些想法(二)
- 计组之中央处理器:6、微指令格式设计、微程序控制单元的设计
- windows server 系统SERVER服务消失无法共享
- 手机视频水印去不掉有马赛克
- 远程控制客户端使用教程-multiDesk
- Request 请求转发
- L-半胱氨酸修饰的金纳米粒子(Cys-GNPs)和牛血清白蛋白/生物素化白蛋白纳米粒
- 帧定格(用于定格画面添加字幕或者图片)
- 各种范文都有,到时不用找了。(值得收藏)
- 强化学习DRL--策略学习(Actor-Critic)
- 【bzoj 1812】[Ioi2005]riv(树形dp)
- 简单工厂和抽象工厂有什么区别?
- 研二小硕的艰难实习路
- Xshell窗口置顶的问题
- 区分车上的点阵屏、彩屏、断码屏
- ffmpeg 硬件加速视频转码指南
热门文章
- 每个程序员都应该参加一次 GDD
- Android开发工具下载及环境配置
- 微软ADFS成本评估
- 勤于奋对国外LEAD项目,不留遗憾,不负此生
- System.currentTimeMillis()的用法
- luogu P6078 [CEOI2004] Sweets(生成函数入门题)
- ptp(precision time protocol)时钟同步
- yeezy350灰橙_阿迪达斯yeezy350灰橙boost鞋底细节,舒服透气吗?
- vc6快10年了,发个vc6能用的SDK和库列表
- asterisk帮助与国内论坛