Fragment專(zhuān)輯(二):Fragment的簡(jiǎn)單用法

在上一篇中簡(jiǎn)單介紹了Fragment,現(xiàn)在來(lái)一個(gè)簡(jiǎn)單的用法

用法

Fragment需要與FrameLayout結(jié)合使用,F(xiàn)rameLayout起到了容器的作用。

首先我們要在布局中使用FrameLayout,設(shè)置相應(yīng)寬高等屬性,它就是我們要添加內(nèi)容的容器,如下面的代碼

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.lzp.MainActivity"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <RadioGroup
        android:id="@+id/tab_radio"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/one"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="1"/>

        <RadioButton
            android:id="@+id/two"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="2"/>

        <RadioButton
            android:id="@+id/three"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="3"/>

    </RadioGroup>

</LinearLayout>

其中FrameLayout就是填充Fragment的容器,當(dāng)要往容器中添加我們需要的Fragment時(shí),需要幾個(gè)步驟:

1、獲取FramgmentTransaction, Fragment的事務(wù)管理類(lèi)
FragmentTransaction transaction = fm.beginTransaction();

2、通過(guò)FragmentTransaction進(jìn)行添加,刪除,替換等操作

3、操作結(jié)束,提交事務(wù),如果不提交,操作無(wú)效。 transaction.commit();
transaction.commitAllowingStateLoss();
稍后再說(shuō)明這兩種提交事務(wù)的方法的不同。

現(xiàn)在是一個(gè)簡(jiǎn)單的例子代碼

package com.example.lzp;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;

public class MainActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener{

    // 負(fù)責(zé)全局的FragmentManager, 建議使用support包中的FramengManager類(lèi)
    private FragmentManager fm;
    private Fragment oneFragment, twoFragment, threeFragment;

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

        fm = getSupportFragmentManager();
        RadioGroup tabRadio = (RadioGroup) findViewById(R.id.tab_radio);
        tabRadio.setOnCheckedChangeListener(this);
        if (oneFragment == null){
            oneFragment = new OneFragment();
        }
        fm.beginTransaction().add(R.id.fragment_container, oneFragment).commit();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction transaction = fm.beginTransaction();
        switch (checkedId){
            case R.id.one:
                transaction.replace(R.id.fragment_container, oneFragment);
                break;

            case R.id.two:
                if (twoFragment == null){
                    twoFragment = new TwoFragment();
                }
                transaction.replace(R.id.fragment_container, twoFragment);
                break;
            case R.id.three:
                if (threeFragment == null){
                    threeFragment = new ThreeFragment();
                }
                transaction.replace(R.id.fragment_container, threeFragment);
                break;
            default:
                break;
        }
        transaction.commit();
    }
}

上面的代碼非常簡(jiǎn)單,自定義三個(gè)Fragment,默認(rèn)添加第一個(gè)Fragment,然后通過(guò)單選框的選擇變化來(lái)添加不同的fragment。

特別注意

FragmentManager推薦使用support包中類(lèi),可以來(lái)兼容比較低的版本,遇到報(bào)錯(cuò)的少年,先看看使用的類(lèi)是否有有問(wèn)題。

另外fragmen還有另外一種使用方法:

<?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">
    <fragment android:name="com.example.news.ArticleListFragment"
           android:id="@+id/list"
           android:layout_weight="1"
           android:layout_width="0dp"
           android:layout_height="match_parent" />
    <fragment android:name="com.example.news.ArticleReaderFragment"
           android:id="@+id/viewer"
           android:layout_weight="2"
           android:layout_width="0dp"
           android:layout_height="match_parent" />
</LinearLayout>

從上面的布局可以看出來(lái),是直接在布局寫(xiě)死了fragment,就好像用積木拼出了一個(gè)完整的布局,但是每一塊都是固定不能變化的,這種用法在手機(jī)開(kāi)發(fā)中并不經(jīng)常使用,畢竟手機(jī)屏幕大小的限制,這種應(yīng)用場(chǎng)景并不常見(jiàn),但是平板開(kāi)發(fā)的少年應(yīng)該對(duì)這種用法非常的了解,把不同功能封裝成模塊進(jìn)行開(kāi)發(fā),使用的時(shí)候直接拼裝,省時(shí)省力。

下一篇我們來(lái)具體說(shuō)明一下Fragment的添加和替換的不同。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容