片段替换

时间:2015-01-14 07:37:08

标签: android android-fragments

对此级别问题感到抱歉, 但我无法弄清楚Fragment是如何工作的.. 我通过ActionBarActivity使用android studio片段的默认值 添加R.id.frag_status时出错 为什么我不能用这种方式来替换或添加片段? 当我按下按钮时,如何将Fragment1和Fragment2替换为R.id.frag_status? 谢谢 enter image description here

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Fragment1 fragment1 = new Fragment1();
        getSupportFragmentManager().beginTransaction()
                .add(R.id.frag_status, fragment1)
                .commit();
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    return rootView;
}
}

和activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context=".MainActivity" tools:ignore="MergeRootFrame" />

fragment_main.xml

  <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$PlaceholderFragment">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/frag_status"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" />

</RelativeLayout>

Fragment1类

package com.fragment.test;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment1, container,false);
    }
}

1 个答案:

答案 0 :(得分:0)

    FragmentTransaction transaction = getFragmentManager().beginTransaction();

    /*transaction.setCustomAnimations(R.animator.enter_anim, R.animator.exit_anim); This line is for animation in transition.
*/
    transaction.replace(R.id.frag_status, new YourFragment());
    transaction.commit();

    /*
    enter_anim.xml:
    <?xml version="1.0" encoding="utf-8"?>
    <set>
         <objectAnimator
             xmlns:android="http://schemas.android.com/apk/res/android"
             android:duration="1000"
             android:propertyName="x"
             android:valueFrom="2000"
             android:valueTo="0"
             android:valueType="floatType" />
    </set>
    exit_anim.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <set>
        <objectAnimator
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:duration="1000"
            android:propertyName="x"
            android:valueFrom="0"
            android:valueTo="-2000"
            android:valueType="floatType" />
    </set>