Fragment的使用

来源:互联网 发布:国债收益率 知乎 编辑:程序博客网 时间:2024/05/17 00:01

一直想把Fragment写到博客中来,可一直没时间。今天抽空把这个Fragment写一下。

Fragment------片段的意思。Fragment是Activity的子模块,也就是Activity中的片段。Fragment必须加载到Activity中才能使用,虽然  Fragment也拥有自己的生命周期,但它的生命周期会受到Activity的生命周期的影响。也就是说当Activity被注销时,那么Activity中的Fragment也都会被销毁。

        使用Fragment有很多好处,因为同一个片段(Fragment)可以被多个Activity重复使用,这也就可以大大提高我们的开发效率。一个Activity可以使用多个Fragment,而一个Fragment可以被多个Activity使用。下面写一个Demo来说明一下Fragment具体是怎么使用的。

设置MainActivity的布局文件:activity_main.xml  

<?xml version="1.0" encoding="utf-8"?><!-- 定义一个水平排列的线性布局 --><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:divider="?android:attr/dividerHorizontal"    android:showDividers="middle">    <fragment         android:id="@+id/left_fragment"        android:name="com.example.test.Left_Fragment"        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="1"/>       <!-- 添加一个FrameLayout容器 -->    <FrameLayout         android:id="@+id/container"        android:layout_width="0dp"        android:layout_height="match_parent"        android:layout_weight="3"/>    </LinearLayout>

MainActivity主页面源码:

public class MainActivity extends Activity implements OnClickListener{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button btn1 = (Button) findViewById(R.id.btn1);Button btn2 = (Button) findViewById(R.id.btn2);btn1.setOnClickListener(this);btn2.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn1:FirstFragment fragment1 = new FirstFragment();FragmentManager fragmentManager1 = getFragmentManager();FragmentTransaction transaction1 = fragmentManager1.beginTransaction();transaction1.replace(R.id.container, fragment1);transaction1.commit();break;case R.id.btn2:SecondFragment fragment2 = new SecondFragment();FragmentManager fragmentManager2 = getFragmentManager();FragmentTransaction transaction2 = fragmentManager2.beginTransaction();transaction2.replace(R.id.container, fragment2);transaction2.commit();break;default:break;}}
左边片段Left_Fragment源码:

public class Left_Fragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view = inflater.inflate(R.layout.left_fragment, container, false);return view;}}
Left_Fragment对应的布局文件:left_fragment.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" >    <Button     android:id="@+id/btn1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="按钮1"    android:textColor="#000"    android:layout_gravity="center_horizontal"/><Button     android:id="@+id/btn2"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="按钮2"    android:textColor="#000"    android:layout_gravity="center_horizontal"/>    </LinearLayout>
右边片段是通过点击左边片段中的两个按钮后动态加载的,此处设置了2个右边片段,由点击“按钮1”和“按钮2”来动态加载。

右边第一个片段FirstFragment源码:

public class FirstFragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view = inflater.inflate(R.layout.first_fragment, container, false);return view;}}
该片段对应的布局文件:first_fragment.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" >        <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:paddingTop="10dp"        android:text="This is the first fragment~"        android:textColor="#000"        android:textSize="20sp"/></LinearLayout>
右边第二个片段SecondFragment源码:

public class SecondFragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view = inflater.inflate(R.layout.second_fragment, container, false);return view;}}
该片段对应的布局文件:second_fragment.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" >        <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:paddingTop="10dp"        android:text="This is the second fragment~"        android:textColor="#000"        android:textSize="20sp"/></LinearLayout>

以上就是我刚写的Demo,下面附带几张真机测试最终结果的图片。

Demo刚开始运行时的界面:


点击“按钮1”之后显示的界面:

点击“按钮2”之后显示的界面:






0 0
原创粉丝点击