package com.tsm.tianshouming.commons; import android.os.Binder; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.text.TextUtils; /** * Created by Tianshouming on 2017/10/24. */
public class FragmentUtils { /** * 类似微信底部table签切换效果, * 一个显示,其他的消失 * * @param fragmentManager * @param from * @param to * @param layoutId */ public static void replaceFragment(FragmentManager fragmentManager, Fragment from, Fragment to, int layoutId) { replaceFragment(fragmentManager, from, to, layoutId, null); } /** * 类似微信底部table签切换效果, * 一个显示,其他的消失 * * @param fragmentManager * @param from * @param to * @param layoutId * @param tag 标签 */ public static void replaceFragment(FragmentManager fragmentManager, Fragment from, Fragment to, int layoutId, String tag) { if (!to.isAdded()) { FragmentTransaction transaction = fragmentManager.beginTransaction(); if (TextUtils.isEmpty(tag)) transaction.hide(from).add(layoutId, to).commit(); else transaction.hide(from).add(layoutId, to, tag).commit(); return; } FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.hide(from).show(to).commit(); } /** * 类似Activity的模式,点击后退键可以将最外层的fragment弹出 * * @param fragmentManager * @param fragment * @param layoutId* 如果需要直接返回到最上一级的fragment,则最上一级的fragment 也需要在回退栈中 或者使用 * fragmentManager.popBackStack(tag,FragmentManager.POP_BACK_STACK_INCLUSIVE); 清空回退栈 */ public static void startFragment(FragmentManager fragmentManager, Fragment fragment, int layoutId) { startFragment(fragmentManager, fragment, layoutId, null); } /** * 类似Activity的模式,点击后退键可以将最外层的fragment弹出 * * @param fragmentManager * @param fragment * @param layoutId * @param tag 标签 *
*
* 如果需要直接返回到最上一级的fragment,则最上一级的fragment 也需要在回退栈中 或者使用 * fragmentManager.popBackStack(tag,FragmentManager.POP_BACK_STACK_INCLUSIVE); 清空回退栈 */ public static void startFragment(FragmentManager fragmentManager, Fragment fragment, int layoutId, String tag) { startFragment(fragmentManager, fragment, layoutId, tag, null); } /** * 类似Activity的模式,点击后退键可以将最外层的fragment弹出 * * @param fragmentManager * @param fragment * @param layoutId * @param tag 标签 * @param bundle 启动的fragment携带的参数 *
*
*
* 如果需要直接返回到最上一级的fragment,则最上一级的fragment 也需要在回退栈中 或者使用 * fragmentManager.popBackStack(tag,FragmentManager.POP_BACK_STACK_INCLUSIVE); 清空回退栈 */ public static void startFragment(FragmentManager fragmentManager, Fragment fragment, int layoutId, String tag, Bundle bundle) { if (fragment.isAdded()) { if (TextUtils.isEmpty(tag)) return; fragmentManager.popBackStack(tag, 0); return; } if (bundle != null) { fragment.setArguments(bundle); } FragmentTransaction transaction = fragmentManager.beginTransaction(); if (TextUtils.isEmpty(tag)) transaction.add(layoutId, fragment).addToBackStack(null).commit(); else transaction.add(layoutId, fragment, tag).addToBackStack(tag).commit(); } /** * 打开fragment 有加载动画 * * @param fragmentManager * @param fragment * @param layoutId * @param tag * @param bundle */ public static void startFragmentWithAnim(FragmentManager fragmentManager, Fragment fragment, int layoutId, String tag, Bundle bundle) { if (fragment.isAdded()) { if (TextUtils.isEmpty(tag)) return; fragmentManager.popBackStack(tag, 0); return; } if (bundle != null) { fragment.setArguments(bundle); } FragmentTransaction transaction = fragmentManager.beginTransaction(); //设置自定义过场动画 transaction.setCustomAnimations(R.anim.popu_enter, R.anim.popu_exit, R.anim.popu_enter, R.anim.popu_exit); if (TextUtils.isEmpty(tag)) transaction.add(layoutId, fragment).addToBackStack(null).commit(); else transaction.add(layoutId, fragment, tag).addToBackStack(tag).commit(); } }