安卓学习笔记之Fragment

每天都会更新一点,我还是学生虽然我知道很多同龄人都很厉害了,所以如果我写了什么不对的地方希望指正一下,谢谢了!每天也会更正一下自己写过的内容。

继Android 3.0系统之后,Google推出了Fragment组件,实际上是解决屏幕大小配置的问题。现在我们了解一下Fragment。网上有些人认为Fragment是安卓第五组件。其实我也是认同的。但是最重要的一点是,他不需要在Manifest清单文件里注册。好了,接下来说说Fragment吧。


什么是Fragment?

Fragment英语单词意思是碎片。实际上就是解决同时能够在手机和平板上显示的问题。不至于,在手机正常排版的格式,运用到平板上就会变得间距不统一导致不自然的问题。在菜鸟教程上看到的图片,
这里写图片描述


Fragment生命周期

我不会画图,为了形象的演示。我只能通过网上查询资料了,这是从菜鸟教程上看来的。通过这张图我们清晰的知道,Fragment生命周期的一个流程。这先说明一点,Fragment这个组件是依附于Activity而存在的,所以再生命周期中我们能看到Activity的相关生命周期方法。
这里写图片描述


Fragment的使用?

Fragment的使用有两种方法。一种是静态,一种是动态。通过代码来演示是如何使用Fragment的。
首先打开Android Studio,创建一个工程。
我们先介绍静态加入两个Fragment。分别创建两个Fragment。相继创建两个XML布局文件。这里补充一点,Fragment有两种包一种是app包下的Fragment,还有V4版本的包,V4包代表的向下兼容。也就是说兼容3.0以下的版本所使用的,但是我想现在估计没什么人使用3.0以下的版本了吧。。。。但是不管怎么样我还是建议使用V4的包吧。毕竟多一事不如少一事好。如果后面发现有什么不对的地方在修改。
这里写图片描述
在Fragment类里分别重写onCreateView()方法,将布局文件添加到里面去。然后在两个布局文件里添加两个TextView来区分fragment1和fragment2

public class Fragment1 extends Fragment {
    private TextView textView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {

        View view=inflater.inflate(R.layout.fragment1,null);
        textView = (TextView) view.findViewById(R.id.textview);
        return view;

    }
}
public class Fragment2 extends Fragment {
    private TextView textView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment2,null);
        textView = (TextView) view.findViewById(R.id.textview);
        return view;

    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="@android:color/darker_gray"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView

        android:id="@+id/textview"
        android:textSize="50sp"
        android:text="This is Fragment1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="@android:color/holo_red_dark"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textview"
        android:textSize="50sp"
        android:text="This is Fragment2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

这里提一下, View view=inflater.inflate(R.layout.fragment1,null);这个代表的是将布局文件转换成View的对象,第一个参数代表的是布局文件,你想将Fragment显示成什么样子在添加哪个布局文件就行了,第二个参数通常为null就行了。

然后返回一个View对象就行了,表示让fragment加载你添加的xml文件。

既然是静态加载Fragment,那么就是相当于添加一个组件那样,相当于加一个Button标签那样简单。需要注意的是,要添加相应的android:name作为标识哪个fragment。贴代码比较清晰一点。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    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" tools:context="com.example.myapplication.MainActivity">

   <fragment
       android:layout_weight="1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:name="com.example.myapplication.Fragment1"
        />
    <fragment
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.example.myapplication.Fragment2"
        />

</LinearLayout>

MainActivity完全不需要做什么事情,只是需要加载一个activity_main文件就行了,因为我们把Fragment当一个普通的组件使用而已。然后点击运行,看效果。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

这里写图片描述
这就是一个静态加载Fragment的方法。
那么动态是怎么样的呢 ?接下来我来讲解一下。


动态加载Fragment

动态添加实际上通过事物这个类来处理,贴上代码。重新修改xml的文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    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" tools:context="com.example.myapplication.MainActivity">


   <Button
       android:id="@+id/add"
       android:text="添加"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />
   <Button
       android:id="@+id/replace"
       android:text="替换"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />
   <Button
       android:id="@+id/dele"
       android:text="删除"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />

   <FrameLayout
       android:id="@+id/frameLayout"
       android:layout_width="match_parent"
       android:layout_height="match_parent"></FrameLayout>


</LinearLayout>

因为这是动态加载的,所以就不能先静态加载那样添加一个标签了。动态的添加分别有add(),replace(),remove()

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button add,replace,dele;
    private Fragment2 f2;
    private Fragment1 f1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        add= (Button) findViewById(R.id.add);
        replace= (Button) findViewById(R.id.replace);
        dele= (Button) findViewById(R.id.dele);
        add.setOnClickListener(this);
        replace.setOnClickListener(this);
        dele.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.add:
                f1 = new Fragment1();
                FragmentManager fm1 = getSupportFragmentManager();
                FragmentTransaction transaction1 = fm1.beginTransaction();
                transaction1.add(R.id.frameLayout, f1).commit();
                break;
            case R.id.replace:
                f2 = new Fragment2();
                FragmentManager fm2 = getSupportFragmentManager();
                FragmentTransaction transaction2 = fm2.beginTransaction();
                transaction2.replace(R.id.frameLayout, f2).commit();
                break;
            case R.id.dele:
                FragmentManager supportFragmentManager = getSupportFragmentManager();
                FragmentTransaction transaction = supportFragmentManager.beginTransaction();
                transaction.remove(f2).commit();
                break;
        }
    }
}

运行的效果如下,点击添加的时候讲Fragment1放进去,
这里写图片描述
当点击替换按钮的时候,
这里写图片描述
当点击删除按钮的时候
这里写图片描述
因为我不会制作gif图片,所以就将就着看吧。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值