1. 程式人生 > >解決fragment來回切換時重複載入耗費資源的問題

解決fragment來回切換時重複載入耗費資源的問題

import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.RadioGroup; import com.example.x1_yue.base.BaseFragment; import com.example.x1_yue.fragment.Classify_Fragment; import com.example.x1_yue.fragment.Discovery_Fragment; import com.example.x1_yue.fragment.Home_Fragment; import com.example.x1_yue.fragment.My_Fragment; import com.example.x1_yue.fragment.Shopping_Fragment; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private int position; private RadioGroup radio_group; private List<Fragment> listfragment; private Fragment mContent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); radio_group = findViewById(R.id.radio_group); innit(); // radio_group.setOnCheckedChangeListener(this); setListener();//設定RadioGroup的監聽 } private void setListener() { radio_group.setOnCheckedChangeListener(new MyOnCheckedChangeListener()); radio_group.check(R.id.ra_btn1);//設定預設選中常用框架 } private void innit() { //個人中心 My_Fragment myfragment = new My_Fragment(); //主頁 Home_Fragment homeFragment = new Home_Fragment(); //分類 Classify_Fragment classifyFragment = new Classify_Fragment(); //發現 Discovery_Fragment discoveryFragment = new Discovery_Fragment(); //購物車 Shopping_Fragment shoppingFragment = new Shopping_Fragment(); //建立集合存放fragment listfragment = new ArrayList<>(); listfragment.add(homeFragment); listfragment.add(classifyFragment); listfragment.add(discoveryFragment); listfragment.add(shoppingFragment); listfragment.add(myfragment); } class MyOnCheckedChangeListener implements RadioGroup.OnCheckedChangeListener { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.ra_btn1: position = 0; break; case R.id.ra_btn2: position = 1; break; case R.id.ra_btn3: position = 2; break; case R.id.ra_btn4: position = 3; break; case R.id.ra_btn5: position = 4; break; default: position = 0; break; } Fragment to = getFragment(); switchFrament(mContent,to); } } /** * @param from 剛顯示的Fragment,馬上就要被隱藏了 * @param to 馬上要切換到的Fragment,一會要顯示 */ private void switchFrament(Fragment from, Fragment to) { if (from!=to){ mContent=to; FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); //才切換 //判斷有沒有被新增 if (!to.isAdded()){ //to沒有被新增 //from隱藏 if (from!=null){ transaction.hide(from); } if (to!=null){ transaction.add(R.id.frag_layout,to).commit(); } }else { //to已經被新增 // from隱藏 if (from!=null){ transaction.hide(from); } //顯示to if (to!=null){ transaction.show(to).commit(); } } } } /** * 根據位置得到對應的Fragment */ private Fragment getFragment() { Fragment fragment = listfragment.get(position); return fragment; } }
xml檔案的佈局 

  <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="11"
            android:id="@+id/frag_layout"
            />
        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:id="@+id/radio_group"
            android:orientation="horizontal"
            >
            <RadioButton
                android:id="@+id/ra_btn1"
                android:drawableTop="@drawable/shou_ye"
                android:drawablePadding="3dp"
                android:gravity="center"
                android:textSize="10sp"
                android:button="@null"
                android:textColor="@drawable/text_color"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:text="首頁"
                />
            <RadioButton
                android:id="@+id/ra_btn2"
                android:button="@null"
                android:drawableTop="@drawable/fen_lei"
                android:drawablePadding="3dp"
                android:gravity="center"
                android:textSize="10sp"
                android:textColor="@drawable/text_color"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:text="分類"
                />
            <RadioButton
                android:id="@+id/ra_btn3"
                android:button="@null"
                android:drawableTop="@drawable/fa_xian"
                android:drawablePadding="3dp"
                android:gravity="center"
                android:textSize="10sp"
                android:textColor="@drawable/text_color"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:text="發現"
                />
            <RadioButton
                android:id="@+id/ra_btn4"
                android:button="@null"
                android:drawableTop="@drawable/shoping_gouwuche"
                android:drawablePadding="3dp"
                android:gravity="center"
                android:textSize="10sp"
                android:textColor="@drawable/text_color"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:text="購物車"
                />
            <RadioButton
                android:id="@+id/ra_btn5"
                android:button="@null"
                android:drawableTop="@drawable/my"
                android:drawablePadding="3dp"
                android:gravity="center"
                android:textSize="10sp"
                android:textColor="@drawable/text_color"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:text="我的"
                />
        </RadioGroup>