(轉)一份關於Fragment的詳細使用教程

Fragment的產生
在Fragment(android 3.0)誕生前,安卓開發中通常使用Activity來展示界面,但它有一定的侷限性:

1、界面中的View控件較多較複雜時,操作麻煩

2、手機和平板中展示的效果不一致


爲了在Android上爲用戶提供動態的、多窗口的交互體驗,我們需要將UI組件和Activity操作封裝成模塊進行使用,使得我們可以在activity中對這些模塊進行切入切出操作,而這些模塊就是Fragment。

Fragment的應用場景

 如上是官方給出的一張圖片,可以看出,平板電腦因爲尺寸較大,展示item詳情的FragmentB直接顯示在了列表右邊區域, 而手機屏幕比較小,故需要跳轉才能看到FragmentB(手機上雖然是兩個Activity,但是用的是同一個FragmentB),類似的具體的例子:手機和平板中的設置界面。

Fragment的使用:

  • 靜態使用Fragment:在activity的XML佈局文件中定義fragment
  • 動態使用Fragment:在activity運行時用代碼動態添加fragment
  • v4包下Fragment的使用:爲了兼容3.0以前的版本,推薦使用v4包下的Fragment

創建和使用Fragment的步驟:

  • 創建子類繼承Fragment
  • 重寫onCreateView()方法定義fragment的佈局
  • 將fragment引入到Activity中
public class NewsFragment extends Fragment {
 
    /**
     * onCreateView是fragment第一次創建繪製用戶界面時系統回調的方法
     * @param inflater 佈局填充器,用於將xml文件轉換成view對象
     * @param container 當前fragment插入activity的佈局視圖對象
     * @param savedInstanceState 存儲上一個fragment的信息
     * @return 返回值View表示當前加載的fragment視圖,若不提供視圖可以返回Null
     */
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //將指定資源的xml文件轉換成具體的view對象
        View view = inflater.inflate(R.layout.fragment_news,null);
        return view;
    }
}

 

在Activity的佈局文件中引用:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.example.limanma.cr7rm.fragment.NewsFragment"/>
 
</LinearLayout>

如上就是靜態使用fragment的過程,非常簡單,接下來看看動態使用:

public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        //1.創建Fragment的管理器對象
        FragmentManager manager = getSupportFragmentManager();
 
        //2.開啓事務
        FragmentTransaction transaction = manager.beginTransaction();
        
        //3.調用事務的動態操作,這裏是添加
        transaction.add(R.id.fl_container,new NewsFragment());
        
        //4.提交事務
        transaction.commit();
    }
}

Fragment的生命週期:(Fragment is added之後,Fragment執行如下方法)

 

 


Fragment和Activity生命週期的對比:

 

 


Fragment與Activity間的通信

  • Activity向Fragment傳值
  • Fragment向Activity傳值
  • Fragment與Fragment間傳遞數據

Activity向Fragment傳值:Activity調用setArguments()傳遞數據,Fragment調用getArguments()接收數據

Fragment向Activity傳值:接口回調,具體步驟如下:

1.在Fragment中定義傳值的回調接口,在生命週期的onAttach()或onCreate()方法中獲取接口的實現

public interface TestInterface{
    void fromFragmentToActivity(String msg);
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    mTestInterface = (TestInterface) getActivity();
}

2.在Fragment需要傳值的位置調用回調接口方法傳值

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news, null);
     
        ...
 
        mTestInterface.fromFragmentToActivity("哈哈哈");
 
        return view;
    }

3.在Activity中實現回調接口並重寫回調方法,獲取傳遞過來的值

public class MainActivity extends AppCompatActivity implements NewsFragment.TestInterface {
 
    ...
 
    @Override
    public void fromFragmentToActivity(String msg) {
        Log.i("TAG","從綁定的Fragment傳遞過來的數據:" + msg);
    }
}

 

Fragment與Fragment間傳遞數據:

 

 

 

ListFragment和DialogFragment的應用
ListFragment繼承於Fragment,ListFragment的內容是以列表(list)的形式顯示的,它的簡單使用可參照如下鏈接:

https://www.kancloud.cn/digest/androidtome/117268

DialogFragment在android 3.0時被引入,官方推薦使用DialogFragment來管理對話框,它的詳細使用可參照如下鏈接:

https://blog.csdn.net/lmj623565791/article/details/37815413
————————————————
版權聲明:本文爲CSDN博主「裏曼馬都」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_35770354/article/details/83477313

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章