码迷,mamicode.com
首页 > 移动开发 > 详细

Android组件--碎片(fragment)

时间:2017-11-10 12:55:09      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:prot   tco   cal   encoding   div   tac   upd   nbsp   mat   

1. 基本概念

一.什么是事务:

事务是应用程序中一系列严密的操作,所有操作必须成功完成,否则在每个操作中所作的所有更改都会被撤消。一个事务中的一系列的操作要么全部成功,要么一个都不做。 事务的结束有两种,当事务中的所以步骤全部成功执行时,事务提交。如果其中一个步骤失败,将发生回滚操作,撤消撤消之前到事务开始时的所以操作。

 

二. 什么是fragment:

Fragment是Android3.0后引入的一个新的API,他出现的初衷是为了适应大屏幕的平板电脑,当然现在他仍然是平板APP UI设计的宠儿,而且我们普通手机开发也会加入这个Fragment,我们可以把他看成一个小型的Activity,又称Activity片段!想想,如果一个很大的界面,我们就一个布局,写起界面来会有多麻烦,而且如果组件多的话是管理起来也很麻烦!而使用Fragment我们可以把屏幕划分成几块,然后进行分组,进行一个模块化的管理!从而可以更加方便的在运行过程中动态地更新Activity的用户界面。另外Fragment并不能单独使用,他需要嵌套在Activity中使用,尽管他拥有自己的生命周期,但是还是会受到宿主Activity的生命周期的影响,比如Activity被destory销毁了,他也会跟着销毁。

 

2. 生命周期

技术分享

3. 代码

注意:因为Fragment是Activity的一部分,所以不需要再manifest.xml中注册。

技术分享

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="sample.android_serialport_api.adapter.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按键1"
        android:id="@+id/fbt1"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按键2"
        android:id="@+id/fbt2"/>

    <FrameLayout
        android:id="@+id/mycontainer"     //在所需要放置Fragment的Activity,添加一个,类似容器
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

</LinearLayout>

fragment_blank.xml
<FrameLayout 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:background="@color/colorPrimary"
    tools:context="sample.android_serialport_api.adapter.BlankFragment1">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="我是framgment一号" />

</FrameLayout>

MainActivity.java
public class MainActivity extends AppCompatActivity {
    Button bt1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt1 = (Button)findViewById(R.id.fbt1);
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                System.out.println("按键1");
                gotoFragment1();
            }
        });
    }

    private void gotoFragment1(){
        System.out.println("Fragment 1");
        //得到Fragment的管理者
        FragmentManager fm = getFragmentManager();
        //开始一个事务
        FragmentTransaction ft = fm.beginTransaction();

        //创建我们的fragment实例
        BlankFragment1 bf = new BlankFragment1();

        ft.replace(R.id.mycontainer, bf);
        ft.commit();
    }
}

BlankFragment1.java
public class BlankFragment1 extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // 返回一个View,因为fragment没有View
        return inflater.inflate(R.layout.fragment_blank, container, false);
    }
}

 技术分享

 

Android组件--碎片(fragment)

标签:prot   tco   cal   encoding   div   tac   upd   nbsp   mat   

原文地址:http://www.cnblogs.com/maogefff/p/7795360.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!