Fragment的兩種加載方式。


一、 靜態加載。

     即用布局文件來加載,

    1. 主類activity類繼承自v4包下的FragmentActivity,

public class MainActivity extends FragmentActivity {

    public MainActivity() {
        Log.e("TAG", "MainActivity()..");
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.e("TAG", "MainActivity onCreate()..");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

 

    2. 定義一個Fragment的子類,並加載一個布局文件。

public class MyFragment1 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        
        //加載布局得到View對象並返回
        
        //創建一個視圖對象, 設置數據並返回
        TextView  textView = new TextView(getActivity());
        textView.setText("fragment11111");
        textView.setBackgroundColor(Color.RED);
        
        return textView;
    }
}
public class MyFragment2 extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        
        //加載布局得到View對象並返回
        
        //創建一個視圖對象, 設置數據並返回
        TextView  textView = new TextView(getActivity());
        textView.setText("fragment22222");
        
        return textView;
    }
}

    3. 在布局文件中通過<fragment>指定自定義的Fragment

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="horizontal">

    <fragment
        android:name="com.example.l12_fragment1.MyFragment1"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent" />

    <fragment
        android:name="com.example.l12_fragment1.MyFragment2"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent" />

</LinearLayout>

 

 每一個Fragment本質上都是一個Framelayout,它加載的布局為其子布局。

     

二、動態加載。

     用代碼來加載,主要API:

     FragmentActivity (getSupportFragmentManager)

     Fragmentmanager (beginTrasacation() )

     FragmentTransacation  (add(),replace(),remove(), commit(),  addToBackStack() )

--------------------------------------------------------------------------------------------------------------------

public class MainActivity extends FragmentActivity {

    public MainActivity() {
        Log.e("TAG", "MainActivity()..");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.e("TAG", "MainActivity onCreate()..");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 創建Fragment對象
        MyFragment1 fragment1 = new MyFragment1();
        // 得到FragmentManager
        FragmentManager manager = getSupportFragmentManager();
        // 得到FragmentTransacation
        FragmentTransaction transaction = manager.beginTransaction();
        // 添加Fragment對象並提交
        transaction.add(R.id.ll_main_container, fragment1).commit();
    }
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="vertical">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="顯示fragment2" 
            android:onClick="showFragment2"/>
        
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="移除fragment2" 
            android:onClick="deleteFragment2"/>
    </LinearLayout>

    <LinearLayout 
        android:id="@+id/ll_main_container"   ////替換的容器
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:orientation="horizontal">
    </LinearLayout>
</LinearLayout>

 

public class MyFragment1 extends Fragment {

    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.e("TAG", "onCreate()");
        super.onCreate(savedInstanceState);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        Log.e("TAG", "onCreateView()");
        //加載布局得到View對象並返回
        
        //創建一個視圖對象, 設置數據並返回
        TextView  textView = new TextView(getActivity());
        textView.setText("fragment11111");
        textView.setBackgroundColor(Color.RED);
        
        return textView;
    }
}

 


免責聲明!

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



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