首页 > 学院 > 开发设计 > 正文

Fragment学习

2019-11-07 23:44:41
字体:
来源:转载
供稿:网友

ps:学习安卓第五天。学习地点:http://www.jianshu.com/p/c3e80b913678

1.Fragment是什么

Fargment类似于Activity,有自己的生命周期。Fargment是镶嵌在Activity中的UI片段。

2.Fragment的生命周期

这里写图片描述

(1)onAttach( )

fragment和activity关联的时候调用。可以在这个方法里进行activity和fragment进行传值。

( 2 ) onCreate( )

创建fragment的时候调用。和activity的onCreate的方法类似。

(3)onCreateView()

加载gramentd界面的时候调用。

(4)onACtivityCreated()

这个方法在onCreated调用完成之后进行。最好不要进行UI方面的操作或者创立。

(5)onStart()

与activity相似,在fragment可见的时候调用。

(6)onResume()

与activity相似,准备与用户交互的时候调用。

(7)onPause()

与activity相似,操作其他的activity,fragment并没有被销毁。

(8)onStop()

与activity相似,fragment不可见的时候调用。

(9)onDestroyView ( )

fragment的布局被销毁的时候调用这个方法。

(10)onDestory()

与activity类似,fragment被销毁的时候调用。

(11)onDetach ( )

fragment与activity解除绑定的时调用这个方法。

ps:其实把生命周期看完的时候,各种不懂。有的资料上说fragment是用来做平板的开发的,也有的 资料上直接把fragment当手机的界面跳转使用。所以判断起来就非常麻烦。

fragment的两种使用方法。

(1)静态使用方法:

ps:学习使用方法的最好的途径就是去写代码。 http://www.jianshu.com/p/c3e80b913678的实例练习:

给我的fragment创建一个布局。可以理解为fragment 的外在显示

<?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="#00ff00"><TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="这里是fragment_first" android:layout_gravity="bottom" android:gravity="center" /></LinearLayout>

布局创建好了之后,就要创建fragment的骨架了,并且要把我们的界面放在fragment中。

public class fragment_first extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v=inflater.inflate(R.layout.fragment_first,container,false); /* 第一个参数的含义为 view添加布局。 第二个参数的含义为 容器。 第三个参数的含义为 确定返回值,如果是false那就是返回了View如果是true的话就不一定返回true。 */ return v; }}

之后就应该将我们的fragment嵌入到activity中去了。感觉在这个fragment更加像一个UI界面的工具和TextView类似的东西。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:orientation="vertical" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.hanlei.fragmenthl.MainActivity"> <TextView android:layout_width="match_parent" android:gravity="center" android:layout_height="100dp" android:text="这是activity" /> <fragment android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fragment_First" android:name="com.example.hanlei.fragmenthl.fragment_first" /></LinearLayout>

ps:感觉静态的fragment已经差不多了。可以继续学习了。突然就有一种学习使我快乐的感觉出来了

(2)fragment的动态创建。

先给fragment建造界面

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.hanlei.fragmenthl1.BlankFragment"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="呈现另一个Fragment" android:id="@+id/btnShowAnotherFragment"/></LinearLayout>

第二个fragment 的界面

<?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"><TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="这是另一个Fragment"/></LinearLayout>

建造fragment

public class BlankFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) { View v=inflater.inflate(R.layout.fragment_blank,container,false); v.findViewById(R.id.btnShowAnotherFragment).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getFragmentManager().beginTransaction().replace(R.id.activity_main,new AnotherFragment()).commit();//跳转到另一个fragment } }); return v; }

另一个fragment建造

public class AnotherFragment extends android.support.v4.app.Fragment{ @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v=inflater.inflate(R.layout.fragment_another,container,false); return v; }}

Activity跳转fragment

public class MainActivity extends AppCompatActivity { @Override PRotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState==null){ getSupportFragmentManager().beginTransaction() .add(R.id.activity_main,new BlankFragment()).commit(); } }}

ps:动态fragment的实例是来自极客学院。

有侧边栏的activity


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表