一個簡單的Android 動態Fragment例項
阿新 • • 發佈:2019-02-13
很簡單的目錄結構,其中fragment2還是做為靜態UI使用,動態載入的是fragment1跟fragment3,這裡對fragment2的程式碼不再展示
先看看fragment1.java跟fragment3.java檔案,這裡沒什麼特別的。
package com.example.root.fragmentproject; import android.app.Activity; import android.net.Uri; import android.os.Bundle; import android.app.Fragment; import android.view.LayoutInflater; import再看看fragment1.xml跟fragment3.xml佈局吧,也沒什麼。android.view.View; import android.view.ViewGroup; public class Fragment1 extends Fragment { // TODO: Rename parameter arguments, choose names that match @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragmentreturn inflater.inflate(R.layout.fragment_fragment1, container, false); } }
<FrameLayout 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"再看看activity_main.xml檔案,其中id為 linear1 佈局就是準備動態載入fragment的地方,前面的靜態fragment就是簡簡單單的兩個按鈕。tools:context="com.example.root.fragmentproject.Fragment1" android:background="#a5a5a5"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="我是對應Button1的Fragment,預設載入的也是我" /> </FrameLayout>
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <fragment android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/id_fragment2" android:name="com.example.root.fragmentproject.Fragment2"> </fragment> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/linear1"> </LinearLayout> </LinearLayout> </RelativeLayout>接下來看看最為關鍵的MainActivity.java檔案。
package com.example.root.fragmentproject; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends FragmentActivity implements OnClickListener { private FragmentManager fragmentManager; private FragmentTransaction fragmentTransaction; private Fragment1 fragment1; private Fragment3 fragment3; private Button button1; private Button button2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); button1.setOnClickListener(this); button2.setOnClickListener(this); fragmentManager = getFragmentManager(); mrFragment(); } //預設載入的fragment public void mrFragment(){ fragmentTransaction = fragmentManager.beginTransaction(); if(fragment1 == null){ fragment1 = new Fragment1(); } fragmentTransaction.replace(R.id.linear1, fragment1); fragmentTransaction.commit(); } @Override public void onClick(View view) { Log.v("debug","進入點選方法。。。。。。。。。。。。。。。。。。。。。"); // 每次都必須重新獲得一個fragmentTransaction,即重新執行下面這行程式碼。 fragmentTransaction = fragmentManager.beginTransaction(); switch (view.getId()){ case R.id.button1: if(fragment1 == null){ fragment1 = new Fragment1(); } fragmentTransaction.replace(R.id.linear1, fragment1); break; case R.id.button2: if(fragment3 == null){ fragment3 = new Fragment3(); } fragmentTransaction.replace(R.id.linear1, fragment3); break; } fragmentTransaction.commit(); } }
最後看看效果吧,首先是當剛進入APP時,以及點選“按鈕1”時:
當點選“按鈕2”時:
該文章主要是為了記錄一點小例項,所以沒有許多更深入的理解或是講解之類的。