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


1.初識Fragment

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

2.Fragment的生命周期

Fragment1和Fragment2

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

 

 

 

3.創建一個Fragment

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類便建完了。

MainActivity.java

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添加到后退棧中

由於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>

 

 項目結構

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM