FragmentActivity中有多个Fragment的效果,这里我们不用ViewPager
MainActivity的代码:
package com.example.fragmentdemo;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private FragmentManager mFragmentManager;
private Fragment1 fragment1;
private Fragment2 fragment2;
private Button button1;
private FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//新建两个Fragment
fragment1 = new Fragment1();
fragment2 = new Fragment2();
mFragmentManager = getFragmentManager();
//Fragment事务
fragmentTransaction = mFragmentManager.beginTransaction();
//增加执行的动画效果 动画不能用我们的Tween动画,后面会有代码
fragmentTransaction.setCustomAnimations(R.anim.animation_view, R.anim.animation_view_exit);
//加入两个Fragment,两个Fragment放入FrameLayout中
fragmentTransaction.add(R.id.myFrameLayout, fragment1);
fragmentTransaction.add(R.id.myFrameLayout, fragment2);
//隐藏第二个先显示第一个
fragmentTransaction.hide(fragment2);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.commit();
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(fragment2.isHidden()){
fragmentTransaction = mFragmentManager.beginTransaction();
//执行动画,隐藏fragment1,显示fragment2
fragmentTransaction.setCustomAnimations(R.anim.animation_view, R.anim.animation_view_exit);
fragmentTransaction.show(fragment2).hide(fragment1);
//一定要commit
fragmentTransaction.commit();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onResume() {
super.onResume();
Log.i("Fragment", "fragment1:" + fragment1.isAdded());
Log.i("Fragment", "fragment2:" + fragment2.isAdded());
}
}
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button1" ></Button> <FrameLayout android:id="@+id/myFrameLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > </FrameLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <objectAnimator android:interpolator="@android:interpolator/accelerate_cubic" android:valueFrom="0" android:valueTo="1" android:valueType="floatType" android:propertyName="alpha" android:duration="2000"/> <objectAnimator android:interpolator="@android:interpolator/accelerate_cubic" android:valueFrom="0.0" android:valueTo="1.0" android:valueType="floatType" android:propertyName="scaleX" android:duration="2000"/> <objectAnimator android:interpolator="@android:interpolator/accelerate_cubic" android:valueFrom="0.0" android:valueTo="1.0" android:valueType="floatType" android:propertyName="scaleY" android:duration="2000"/> </set>Fragment隐藏时动画代码:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <objectAnimator android:interpolator="@android:interpolator/accelerate_cubic" android:valueFrom="1" android:valueTo="0" android:valueType="floatType" android:propertyName="alpha" android:duration="2000"/> <objectAnimator android:interpolator="@android:interpolator/accelerate_cubic" android:valueFrom="1.0" android:valueTo="0.0" android:propertyName="ScaleX" android:duration="2000" android:valueType="floatType" /> <objectAnimator android:interpolator="@android:interpolator/accelerate_cubic" android:valueFrom="1.0" android:valueTo="0.0" android:valueType="floatType" android:propertyName="ScaleY" android:duration="2000" /> </set>
两个Fragment的代码
Fragment1的代码
package com.example.fragmentdemo; import android.annotation.SuppressLint; 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 View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View fragment1 = inflater.inflate(R.layout.fragment1, null); return fragment1; } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#FFFF0000" > </LinearLayout>
package com.example.fragmentdemo; import android.annotation.SuppressLint; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment2 extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View fragment1 = inflater.inflate(R.layout.fragment2, null); return fragment1; } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#FFFF00FF" > <TextView android:id="@+id/ftv2" android:layout_width="match_parent" android:layout_height="match_parent" android:text="dsfadsfasdfdas" /> </LinearLayout>
小结,通过FragmentTransaction来显示和隐藏Fragment并有动画效果
代码下载