是的,你没有看错,是(五),你也不用再找了,这里没有一二三四,直接就是五啦,由于一二三四在这里。。。很久没写安卓的博客了,以前定好的要在2016年前写完这个系列的目标也是失败了,但文章总得要写,这篇就是关于fragment的简单使用。html
fragment的意思就是碎片,是一种能够嵌入在activity中的UI片断,能够最大化的利用手机的屏幕。如今不少应用都使用了fragment,最多见的就是微信了,微信下面的tab栏切换时,上面的内容就是用fragment来显示的。看看看,这么牛的微信都在用fragment,咱们赶忙来用他吧。android
啥,又是生命周期,他又不是activity,怎么还有生命周期呢。是的,fragment依托于activity而存在,他是有生命周期的,并且他的生命周期的理解最好仍是和activity一块儿比较更容易理解。在说明他的生命周期以前,咱们先来实现一个简单的fragment的例子。微信
咱们先来写两个简单的fragment,而后在MainActivity点击按键来切换这两个fragment。ide
第一个fragment布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:gravity="center" android:background="@android:color/holo_blue_light" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="第一个Fragment" android:textSize="25sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
第二个fragment学习
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:gravity="center" android:background="@android:color/holo_red_light" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="第二个Fragment" android:textSize="25sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
能够看到这两个布局都很是简单,只是单纯的设置了背景色以及一个TextView。
而后实现这两个fragment,新建两个类FirstFragment和SecondFragment都继承Fragment,并将布局文件和类进行绑定。
FristFragment:ui
public class FirstFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.first_fragment,null); return view; } }
SecordFragment:spa
public class SecondFragment extends Fragment{ @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.second_fragment,null); return view; } }
接下来是MainActivity的布局.net
<?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"> <LinearLayout android:layout_marginTop="10dp" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:gravity="center" android:id="@+id/btn_change" android:text="Change" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <FrameLayout android:id="@+id/framelayout" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:id="@+id/fragment" android:name="com.example.jiang.fragmenttest.FirstFragment" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout> </LinearLayout>
这里使用了FrameLayout,他的特性就是会从左上角层叠布局。默认显示的是FirstFragmentcode
接下来看MainActivity的内容,给button添加了点击事件:
public class MainActivity extends AppCompatActivity { private Button btn_change; private SecondFragment secondFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_change = (Button) findViewById(R.id.btn_change); btn_change.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { secondFragment = new SecondFragment(); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.framelayout,secondFragment); transaction.commit(); } }); } }
这样就能够实现点击切换fragment的效果,先看效果图:
这里咱们能够看到点击按键后界面就进行切换了,这里一个动态的fragment的切换主要是如下五个步骤:
建立一个fragment实例,
获取FragmentManager,在activity中能够直接调用getFragmentManager()
来获取,
开启一个事务,经过beginTransaction()
来开启,
向容器添加fragment,通常采用replace()
来切换,须要传入容器的id以及要替换的fragment实例,
提交事务,使用commit()
方法。
这样就是能够动态的切换一个fragment。这里咱们发如今咱们切换了fragment后再按下返回键,程序居然不是返回到第一个fragment而是直接退出了。这样很不符合咱们的操做习惯。这里咱们就能够把事务加入到返回栈中。修改MainActivity中的代码,我这里就不贴全了。
transaction.replace(R.id.framelayout,secondFragment); transaction.addToBackStack(null); transaction.commit();
从新编译一次,就发现当按下返回键时就返回到了第一个fragment了。这里就不贴图了。
这只是一个最简单的例子,咱们就来参照这个例子来看一下fragment的生命周期。fragment是依托于activity的,那么他的生命周期就与activity息息相关,咱们先来看看官网fragment讲解上的一张图片。
先不着急说明,先到代码中将生命周期打印出来。打开程序:
FirstFragment首次加载,生命周期是onAttach()
-->onCreate()
-->onCreateView()
-->onActivityCreated()
-->onStart()
-->onResume
。
而后咱们点击button去切换到SecondFragment:
咱们看到FirstFragment先执行onPause()
-->onStop()
-->onDestoryView()
。而后SecondFragment再执行。咱们再按返回键返回到FirstFragment:
看这里SecondFragment在执行完onDestoryView()
后又执行了onDestory()
-->onDetach()
。FirstFragment则直接从onActivityCreated()
开始执行。接着咱们再按返回键退出程序:
这里FirstFragment才去执行onDestoryView()
等方法。咱们再看官网上的另外一张图
这样这张图是否是更好理解了呢。这里我没有把activity的生命周期也加入打印,但愿有条件的同窗本身去敲敲代码,看看二者间的关系是否是像第一张图那样的。
这样最简单的一个fragment就写完了,但这基本上没有什么实际意义,下一篇文章将会写一个简单的底部tab栏切换,使用viewpaper+fragment。
耽搁了这么久才写完这篇,目标也是没有达到。这大半年间也是经历了许多,工做,生活都是有了大的变化,有开心,也有失落。从新捡起未写完的博客,每一个人都在拼搏,有时候看到些负能量的段子--“比我聪明的人还在努力,我他妈的努力还有什么用”,微微一笑,是啊,这里确实比我还优秀的人都比我还努力,我努力还有什么用呢。安下心来,认清本身只是个普通人,不要总活在梦中,脚踏实地的干活,学习。罗马不是一天建成的,腾讯帝国不是一蹴而就的,谁没有经历过失败呢。一步一步的走,一步一步的犯错,一步一步的改正,一步一步的走向成功。路飞用了两年来升级,我给本身三年时间来升级。北京升级之路。(吐槽:这最后一段老是废话,还不连贯,整个思想都是乱的,你这叫我怎么看(╯‵□′)╯︵┻━┻,再很差好写文章我就取关啦,哼!)哈哈,大家是否是这么想,要是我猜对了就果断给我点波关注呗,会继续写android以及React-Native的文章。谢谢啦!
学习路漫漫,吾将上下而求索。