Android之靜態和動態加載Fragment


一、fragment的靜態加載和動態加載的理解:

         我覺得可以把fragment當做為一個自定義的布局,然后去使用這個定義好的布局,對於靜態和動態的理解是,靜態是講在主布局文件中放入fragment布局,然后使用,而動態是不需要在主布局文件中去聲明fragment的,而是直接在java代碼中去添加。

二、通過一個例子來理解:

說明下下面的圖:第一個為主界面,通過兩個按鈕來分別實現靜態和動態效果

                  

靜態實現:

定義一個fragment_layout布局文件作為靜態添加的對象:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:background="#aabbcc"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
   <TextView
       android:textSize="30sp"
       android:text="這是靜態加載的fragment"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />
</LinearLayout>

定義一個MyFragment類,繼承Fragment:

public class MyFragment extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        //定義一個view對象,並返回
        //第一個參數為上面定義的布局文件,第二參數為傳入的參數,第三個參數一般設為false
        View view=inflater.inflate(R.layout.fragment_layout,container,false);
        return view;
    }
}

創建一個主布局文件activity_main2中放入fragment:(注意這里添加的Fragment要加入name屬性)

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

    <fragment
       
android:id="@+id/myfragment"
       
android:layout_width="match_parent"
       
android:layout_height="match_parent"
       
android:name="base.learnfragment.MyFragment"/>

</LinearLayout>

然后再創建一個ActivityMain2加載第二個活動的主布局:

public class ActivityMain2 extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    }
}

至此,一個靜態的fragment就實現 了。

下面我們來補充第一個活動的主布局activity_main:(注意這里要加入一個id:container(可以自己定義))

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:orientation="vertical"
   
android:id="@+id/container"
   
tools:context="base.learnfragment.MainActivity">


    <Button
       
android:text="靜態加載fragment"
       
android:id="@+id/btn_static"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content" />

    <Button
       
android:text="動態加載fragment"
       
android:id="@+id/btn_dongtai"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content" />

</LinearLayout>

定義第二個類MyFragment2,以及布局文件fragment_layout2:

public class MyFragment2 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_layout2,container,false);
        return view;
    }
}
 
<LinearLayout
   
android:background="#ff0000"
   
xmlns:android="http://schemas.android.com/apk/res/android"
   
android:orientation="vertical"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent">

    <TextView
       
android:textSize="30sp"
       
android:text="這是動態加載的fragment"
       
android:layout_width="match_parent"
       
android:layout_height="match_parent" />

</LinearLayout>

補充MainActivity:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_static).setOnClickListener(this);
        findViewById(R.id.btn_dongtai).setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_static:
                //跳轉到第二個活動,使第二活動完成靜態加載fragment
               
startActivity(new Intent(MainActivity.this,ActivityMain2.class));

                break;
            case R.id.btn_dongtai:
                //聲明我們自定義的fragment
               
MyFragment2 fragment2=new MyFragment2();

                //動態添加到布局中,注意replace中的第一個參數為activity_main中的id
               
getSupportFragmentManager().beginTransaction().replace(R.id.container,fragment2).commit();

                break;
        }
    }
}

這樣動態添加也完成了。

總結一下:

1.注意每一個fragment的布局都要有一個與之對應的繼承於Fragment的類。

2.關於繼承.app.Fragment和.support.v4.app.Fragment的問題:

如果要使用前者,那么MainActivity繼承Activity即可,然后動態添加時,是getFragmetManager();

如果使用后者,那么MainActivity要繼承FragActivty,然后動態添加時,是getSupportFragmentManager();

 


免責聲明!

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



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