如何用Fragment中的Button替换Fragment?

时间:2014-09-10 11:59:13

标签: android android-fragments replace

我想替换片段。

我添加了&在java代码中替换了片段。

这是我的main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/fragmentView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/buttonLayout" >
    </FrameLayout>

    <LinearLayout
        android:id="@+id/buttonLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" >

        <Button
            android:id="@+id/btnHome"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/home" />

        <Button
            android:id="@+id/btnPass"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/afragment" />
    </LinearLayout>

</RelativeLayout>

这是我的UI捕获。

enter image description here enter image description here

我可以用底部按钮替换片段。

我想用你看到的HOME片段内的按钮替换'A片段','B片段','C片段'。

这是我的HomeFragment。

public class HomeFragment extends Fragment implements OnClickListener {

    BaseFragmentActivity baseFragmentActivity;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.homefragment, container, false);
        baseFragmentActivity = new BaseFragmentActivity();
        root.findViewById(R.id.btnGoGame).setOnClickListener(this);
        root.findViewById(R.id.btnGoMusic).setOnClickListener(this);
        root.findViewById(R.id.btnGoShopping).setOnClickListener(this);
        return root;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btnA:
            baseFragmentActivity.fragmentReplace(baseFragmentActivity.A_FRAGMENT);
            break;

        case R.id.btnB:
            baseFragmentActivity.fragmentReplace(baseFragmentActivity.B_FRAGMENT);
            break;

        case R.id.btnC:
            baseFragmentActivity.fragmentReplace(baseFragmentActivity.C_FRAGMENT);
            break;

        default:
            break;
        }       
    }
}

&安培; BaseFragmentActivity代码。

public class BaseFragmentActivity extends FragmentActivity{ 

    public final int HOME_FRAGMENT = 0;
    public final int A_FRAGMENT = 1;
    public final int B_FRAGMENT = 2;
    public final int C_FRAGMENT = 3;

    private HomeFragment homeFragment;
    private AFragment aFragment;
    private BFragment bFragment;
    private CFragment cFragment;

    public void fragmentReplace(int fragIndex){

        FragmentManager fragManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragManager.beginTransaction();
        transaction.replace(R.id.fragmentView, getFragment(fragIndex));
        transaction.addToBackStack(null);
        transaction.commit();   
    }

    private Fragment getFragment(int index) {

        Fragment newFragment = null;
        switch (index) {
        case HOME_FRAGMENT:
            if(homeFragment == null) {
                homeFragment = new HomeFragment();
            }
            newFragment = homeFragment;
            break;

        case A_FRAGMENT:
            if(aFragment == null) {
                aFragment = new AFragment();
            }
            newFragment = aFragment;
            break;

        case B_FRAGMENT:
            if(bFragment == null) {
                bFragment = new BFragment();
            }
            newFragment = bFragment;
            break;

        case C_FRAGMENT:
            if(cFragment == null) {
                cFragment = new CFragment();
            }
            newFragment = cFragment;
            break;

        default:
            break;
        }

        return newFragment;
    }
}

但如果我触摸A,B,C按钮,我就会出错。

09-10 20:15:28.943: E/AndroidRuntime(16262): FATAL EXCEPTION: main
09-10 20:15:28.943: E/AndroidRuntime(16262): Process: com.sa.fragmenttest, PID: 16262
09-10 20:15:28.943: E/AndroidRuntime(16262): java.lang.IllegalStateException: Activity has been destroyed
09-10 20:15:28.943: E/AndroidRuntime(16262):    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1365)
09-10 20:15:28.943: E/AndroidRuntime(16262):    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
09-10 20:15:28.943: E/AndroidRuntime(16262):    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
09-10 20:15:28.943: E/AndroidRuntime(16262):    at com.sa.tpass_sa.BaseFragmentActivity.fragmentReplace(BaseFragmentActivity.java:49)
09-10 20:15:28.943: E/AndroidRuntime(16262):    at com.sa.tpass_sa.fragments.HomeFragment.onClick(HomeFragment.java:44)
09-10 20:15:28.943: E/AndroidRuntime(16262):    at android.view.View.performClick(View.java:4461)
09-10 20:15:28.943: E/AndroidRuntime(16262):    at android.view.View$PerformClick.run(View.java:18520)
09-10 20:15:28.943: E/AndroidRuntime(16262):    at android.os.Handler.handleCallback(Handler.java:733)
09-10 20:15:28.943: E/AndroidRuntime(16262):    at android.os.Handler.dispatchMessage(Handler.java:95)
09-10 20:15:28.943: E/AndroidRuntime(16262):    at android.os.Looper.loop(Looper.java:136)
09-10 20:15:28.943: E/AndroidRuntime(16262):    at android.app.ActivityThread.main(ActivityThread.java:5103)
09-10 20:15:28.943: E/AndroidRuntime(16262):    at java.lang.reflect.Method.invokeNative(Native Method)
09-10 20:15:28.943: E/AndroidRuntime(16262):    at java.lang.reflect.Method.invoke(Method.java:515)
09-10 20:15:28.943: E/AndroidRuntime(16262):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
09-10 20:15:28.943: E/AndroidRuntime(16262):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
09-10 20:15:28.943: E/AndroidRuntime(16262):    at dalvik.system.NativeStart.main(Native Method)

有可能吗?怎么做?

1 个答案:

答案 0 :(得分:1)

试试这个。也许它会帮助你

    public void onClick(View v) {
            switch (v.getId()) {
            case R.id.btnA:
    ((BaseFragmentActivity)getActivity()).fragmentReplace(baseFragmentActivity.A_FRAGMENT);         break;

            case R.id.btnB:
                ((BaseFragmentActivity)getActivity()).fragmentReplace(baseFragmentActivity.B_FRAGMENT);
                break;

            case R.id.btnC:
               ((BaseFragmentActivity)getActivity()).fragmentReplace(baseFragmentActivity.C_FRAGMENT);
                break;

            default:
                break;
            }       
        }