進階篇-用戶界面:1.Fragment-初識Fragment組件

1.初識Fragmentjava

      Fragment的中文含義是碎片,在以前安卓開發是,用戶界面的切換所有使用activity的切換,這就形成了整個應用使用起來速度較慢,並且很佔內存,由於activity是重量級的組件,在應用程序內部使用很不方便。因而出現了Fragment來解決這樣的問題。Fragment是一種便捷的、輕量級的、基於activity的組件,所謂基於activity,就是必須有activity做爲容器,Fragment才能夠生存。另外,Fragment 能夠動態的添加和刪除。因此說Fragment是一種很是方便開發的組件 。android

2.Fragment的生命週期app

Fragment1和Fragment2ide

主界面默認加載1,從1跳轉到2時,2執行onCreate-onCreateView,後退時執行onPause-onDesdory。1執行onPause-onDesdroyView。再點後退,1執行OnDestory。佈局

 

 

 

3.建立一個Fragmentspa

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by lzc on 16/6/27.
 */
public class Frag2 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment2,container,false);
        v.findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getFragmentManager().popBackStack();
            }
        });
        return v;
    }
}

建立一個自定義的類,繼承Fragment類,包爲android.support.v4.app.Fragment。這個類裏有一個默認的onCreate方法,這個方法要返回一個view類型的對象。這個對象的值爲inflater對象調用inflate方法返回的值。第一個參數是此Fragment所加載的xml佈局文件。也就是一個Fragment對應一個xml佈局文件。建立完View對象後, 最後返回此對象,一個Fragment類便建完了。code

MainActivity.javaxml

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(savedInstanceState == null){
            getSupportFragmentManager().beginTransaction().add(R.id.container,new Frag1()).commit();
        }
    }
}

使用FragmentManager將Fragment添加到主佈局裏。(若是要切換Fragment,把add方法改爲replace方法)。另外,加載Fragment的主類所加載xml佈局爲FramLayout。R.id.container爲主佈局中FramLayout的id。對象

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/container"

4.將Fragment添加到後退棧中blog

因爲fragment是基於activity的,因此同一個activity內部的fragment切換之後,點擊返回鍵不會回到上一個Fragment,而是回到上一個Activity,或者退回主界面。

添加到安卓手機回退鍵的回退棧中:

 getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.container,new Frag2()).commit();

添加按鈕使Fragment回退:

 

getFragmentManager().popBackStack();

 

5.實現同一個Activity內兩個Fragment的跳轉源碼

Frag1.java

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by lzc on 16/6/27.
 */
public class Frag1 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment1,container,false);
        v.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.container,new Frag2()).commit();
            }
        });
        return v;
    }
}

fragment1.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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fragment1"
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳轉Fragment"
        android:id="@+id/button" />

</LinearLayout>

Frag2.java

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by lzc on 16/6/27.
 */
public class Frag2 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment2,container,false);
        v.findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getFragmentManager().popBackStack();
            }
        });
        return v;
    }
}

fragment2.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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fragment2"
        android:id="@+id/textView3" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="back"
        android:id="@+id/button2" />
</LinearLayout>

MainActivity.java見上文圖

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>


    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="52dp">

</FrameLayout>

 

 項目結構

相關文章
相關標籤/搜索