Fragment动画切换效果
移动开发
2018-05-31 18:06:53
阅读次数: 0
1.系统自带的动画切换效果
FragmentTransaction fragmentTransaction = mFragmentManager
.beginTransaction();
fragmentTransaction.setTransition(int transit);
2. 自定义动画切换效果
FragmentTransaction fragmentTransaction = mFragmentManager
.beginTransaction();
fragmentTransaction.setCustomAnimations(
R.anim.in_from_right,
R.anim.out_to_left);
public class Main2Activity extends AppCompatActivity implements View.OnClickListener{
private TextView tv_home,tv_person;
private FrameLayout fl;
private FragmentManager mFragmentManager;
private ItemFragment mItemFragment;
private HomeFragment mHomeFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
}
private void initView() {
tv_home=findViewById(R.id.tv_home);
tv_person=findViewById(R.id.tv_person);
fl=findViewById(R.id.fl);
tv_home.setOnClickListener(this);
tv_person.setOnClickListener(this);
mFragmentManager = getSupportFragmentManager();
mHomeFragment=HomeFragment.newInstance();
mItemFragment=ItemFragment.newInstance();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.tv_home:
switchPage(mHomeFragment);
break;
case R.id.tv_person:
switchPage(mItemFragment);
break;
default:
break;
}
}
//切换页面 (自定义fragment切换动画)
private void switchPage(Fragment fragment) {
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction
.setCustomAnimations(R.anim.right_in,R.anim.left_out)
.replace(R.id.fl,fragment)
.commit();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
tools:context="meiyuan.com.text01.Main2Activity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ccc"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_home"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:text="首页"
android:gravity="center"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#f00"/>
<TextView
android:id="@+id/tv_person"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:text="个人中心"
android:gravity="center"/>
</LinearLayout>
<FrameLayout
android:id="@+id/fl"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
转载自blog.csdn.net/oneblue123/article/details/80510237