1. 程式人生 > ><Android基礎>(四) Fragment Part 1

<Android基礎>(四) Fragment Part 1

ace 開啟 bundle textview contain ima tro aci 簡單的

Fragment

1)Fragment的簡單用法

2)動態添加Fragment

3)在Fragment中模擬返回棧

4)Fragment和活動之間通信

第四章 Fragment

Fragment是一種可以嵌入在活動當中的UI片段,它能讓程序更加合理和充分的利用大屏幕的空間,比如橫屏和平板。

4.2 Fragment的使用方式

創建一個平板模擬器

技術分享圖片

4.2.1 Fragment的簡單用法

兩個Fragment平分活動,新建一個FragmentTest項目。

1.左側fragment與右側fragment的布局創建

新建一個left_fragment.xml,放置一個按鈕並居中

<?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">

    <Button
        android:id="@+id/button"
        android:layout_width
="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Button"/> </LinearLayout>

新建一個right_frgment.xml,放置一個TextView用於顯示文本

<?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="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:textSize="20dp" android:text="This is right fragment"/> </LinearLayout>

2.新建左fragment和右fragment的類,繼承自Fragment

(推薦使用android.support.v4.app.Fragment,能讓Fragment在所有Android系統版本中保持功能一致性)

新建一個LeftFragment類,繼承自Fragment

重寫了onCreatView()方法,通過inflate()方法將剛才定義的left_fragment布局動態加載進來

public class LeftFragment extends Fragment {
    
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.left_fragment, container, false);
        return view;
    }
}

同理,建立一個RightFragment類,繼承自Fragment

public class RightFragment extends Fragment{
    
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.right_fragment, container, false);
        return view;
    }
}

3.修改activity_main.xml文件中的代碼

元素設置為水平放置,用<fragment>標簽在布局中添加fragment,通過android:name屬性來顯示指明添加的碎片類名

<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:id="@+id/left_fragment"
        android:name="com.example.song.fragmenttest.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <fragment
        android:id="@+id/right_fragment"
        android:name="com.example.song.fragmenttest.RightFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

</LinearLayout>

運行程序:

技術分享圖片

兩個碎片平分了整個活動

4.2.2 動態添加碎片

1.新建another_right_fragment.xml

和right_fragment.xml中額代碼基本相同,改了背景色和顏色

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#ffff00"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="20sp"
        android:text="This is another right Fragment" />

</LinearLayout>
    

2.新建AnotherRightFragment作為另一右側碎片

public class AnotherRightFragment extends Fragment {
    
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.another_right_fragment, container, false);
        return view;
    }
}

3.動態的加入activity_main.xml中

將右側fragment替換為了<FramLayout>(Android中最簡單的布局,所有控件會默認擺放在布局的左上角),即將another_right_fragment以FrameLayout的形式放在右側

<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:id="@+id/left_fragment"
        android:name="com.example.song.fragmenttest.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

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

    </FrameLayout>


</LinearLayout>

4.修改MainActivity中的代碼

當點擊左側fragment中的按鈕時,會調用replaceFragment()方法將右側碎片替換成AnotherRightFragment

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        replaceFragment(new RightFragment());
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                replaceFragment(new AnotherRightFragment());
            }
        });
    }

    private void replaceFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.right_layout, fragment);
        transaction.commit();

    }
}

動態添加碎片主要分5步:

(1)創建待添加的fragment實例

(2)獲取FragmentManager,在活動中可以直接通過調用getSupportFragmentManager()方法得到

(3)開啟一個事務,通過調用beginTransaction()方法來開啟

(4)向容器內添加或替換fragment,一般使用replace()方法,需要傳入容器的id和待添加的fragment實例

(5)提交事務,通過commit()方法完成

運行程序:

技術分享圖片

技術分享圖片

4.2.3 在fragment中模擬返回棧

按下back鍵返回上一個Fragment

在FragmentTransacition提供一個addToBackStack()方法即可,用於將一個實務添加到返回棧中,傳入null即可。

    private void replaceFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.right_layout, fragment);
        transaction.addToBackStack(null);
        transaction.commit();

    }

4.2.4 fragment和活動之間的通信

1.活動調用fragment裏的方法

RightFragment rightFragment = (RightFragment) getSupportFragmentManager().
findFragmentById(R.id.right_fragment);

可以得到相應fragment的實例然後調用其中的方法。

2.fragment調用活動中的方法

MainActivity activity = (MainActivity) getActivity();

可以得到相應活動的實例然後調用其中的方法。

3.當fragment需要使用Context對象時,亦可以使用getActivity()方法,因為獲取到的活動本身為一個Context對象。

<Android基礎>(四) Fragment Part 1