實現思路
1.寫一個父類布局,里面寫一個按鍵和一個幀布局(用於給Fragment布局后續替代)
2.寫3個子布局,並且在寫3個class繼承Fragment布局
3.在MainActivity的class中寫替換碎片布局的方法
(包含:FragmentManger(碎片管理器)、getSupportFragmentManager(得到支持碎片管理器)、FragmenTransaction(碎片交換器)、beginTransaction(開始碎片交換)、replace(替換)、commit(交付))
寫一個父類布局,里面寫一個按鍵和一個幀布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/Button1" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:text="改變下面的碎片"/> <FrameLayout android:id="@+id/FrameLayout1" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="8"> </FrameLayout> </LinearLayout>
寫3個子布局,並且在寫3個class繼承Fragment布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp"> <ImageView android:id="@+id/fragment_1_ImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/ace"/> <TextView android:id="@+id/fragment_1_TextView" android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/ace" android:textColor="@color/colorBlack" android:layout_marginTop="20dp"/> </LinearLayout>

相同布局在寫2個
寫一個class繼承fragment實現碎片布局實例化
package com.example.lenovo.myfragmentdemo2.fragment; // 注意Fragment的依賴庫建議統一使用support-v4庫中的 import android.support.v4.app.Fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.example.lenovo.myfragmentdemo2.R; /** * Created by lenovo on 2018/5/7. */ public class Fragment1 extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_1,container,false); return view; } }
在MainActivity的class中寫替換碎片布局的方法
package com.example.lenovo.myfragmentdemo2; // 注意Fragment的依賴庫 import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import com.example.lenovo.myfragmentdemo2.fragment.Fragment1; import com.example.lenovo.myfragmentdemo2.fragment.Fragment2; import com.example.lenovo.myfragmentdemo2.fragment.Fragment3; public class MainActivity extends AppCompatActivity { private Button button; private int i = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.Button1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { switch (i){ case 1: replaceFragment(new Fragment1()); i++; break; case 2: replaceFragment(new Fragment2()); i++; break; case 3: replaceFragment(new Fragment3()); i++; break; default: replaceFragment(new Fragment1()); i=1; break; } } }); } public void replaceFragment(Fragment fragment){ FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction();//注意!每次要執行commit()方法的時候都需要重新獲取一次FragmentTransaction,否則用已經commit過的FragmentTransaction再次commit會報錯! transaction.replace(R.id.FrameLayout1,fragment); transaction.commit(); } }
運行效果:
點擊后:
