标签:sid getch 布局 rate commit array 显示 removing etl
- Fragment相关→FragmentUtils.java→Demo
addFragment : 新增fragment removeFragment : 移除fragment replaceFragment : 替换fragment popFragment : 出栈fragment popToFragment : 出栈到指定fragment popAddFragment : 先出栈后新增fragment hideFragment : 隐藏fragment showFragment : 显示fragment hideShowFragment : 先隐藏后显示fragment getLastAddFragment : 获取最后加入的fragment getLastAddFragmentInStack: 获取栈中最后加入的fragment getTopShowFragment : 获取顶层可见fragment getTopShowFragmentInStack: 获取栈中顶层可见fragment findFragment : 查找fragment getPreFragment : 获取目标fragment的前一个fragment getFragments : 获取同级别的fragment getAllFragments : 获取所有fragment getAllFragmentsInStack : 获取栈中所有fragment setBackgroundColor : 设置背景色 setBackgroundResource : 设置背景资源 setBackground : 设置背景
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2017/1/17
* desc : Fragment相关工具类
* </pre>
*/
public class FragmentUtils {
private FragmentUtils() {
throw new UnsupportedOperationException("u can‘t instantiate me...");
}
private static final int TYPE_ADD_FRAGMENT = 0x01 << 0;
private static final int TYPE_REMOVE_FRAGMENT = 0x01 << 1;
private static final int TYPE_REPLACE_FRAGMENT = 0x01 << 2;
private static final int TYPE_HIDE_FRAGMENT = 0x01 << 3;
private static final int TYPE_SHOW_FRAGMENT = 0x01 << 4;
private static final int TYPE_HIDE_SHOW_FRAGMENT = 0x01 << 5;
private static final int TYPE_POP_ADD_FRAGMENT = 0x01 << 6;
private static final String ARGS_ID = "args_id";
private static final String ARGS_IS_HIDE = "args_is_hide";
private static final String ARGS_IS_ADD_STACK = "args_is_add_stack";
/**
* 新增fragment
*
* @param fragmentManager fragment管理器
* @param containerId 布局Id
* @param fragment fragment
* @return fragment
*/
public static Fragment addFragment(@NonNull FragmentManager fragmentManager,
@NonNull Fragment fragment,
int containerId) {
return addFragment(fragmentManager, fragment, containerId, false);
}
/**
* 新增fragment
*
* @param fragmentManager fragment管理器
* @param containerId 布局Id
* @param fragment fragment
* @param isHide 是否显示
* @return fragment
*/
public static Fragment addFragment(@NonNull FragmentManager fragmentManager,
@NonNull Fragment fragment,
int containerId,
boolean isHide) {
return addFragment(fragmentManager, fragment, containerId, isHide, false);
}
/**
* 新增fragment
*
* @param fragmentManager fragment管理器
* @param containerId 布局Id
* @param fragment fragment
* @param isHide 是否显示
* @param isAddStack 是否入回退栈
* @return fragment
*/
public static Fragment addFragment(@NonNull FragmentManager fragmentManager,
@NonNull Fragment fragment,
int containerId,
boolean isHide,
boolean isAddStack) {
putArgs(fragment, new Args(containerId, isHide, isAddStack));
return operateFragment(fragmentManager, null, fragment, TYPE_ADD_FRAGMENT);
}
/**
* 新增fragment
*
* @param fragmentManager fragment管理器
* @param containerId 布局Id
* @param fragment fragment
* @param isAddStack 是否入回退栈
* @param sharedElement 共享元素
* @return fragment
*/
public static Fragment addFragment(@NonNull FragmentManager fragmentManager,
@NonNull Fragment fragment,
int containerId,
boolean isAddStack,
SharedElement... sharedElement) {
putArgs(fragment, new Args(containerId, false, isAddStack));
return operateFragment(fragmentManager, null, fragment, TYPE_ADD_FRAGMENT, sharedElement);
}
/**
* 移除fragment
*
* @param fragment fragment
* @return fragment
*/
public static Fragment removeFragment(@NonNull Fragment fragment) {
return operateFragment(fragment.getFragmentManager(), null, fragment, TYPE_REMOVE_FRAGMENT);
}
/**
* 替换fragment
*
* @param srcFragment 源fragment
* @param destFragment 目标fragment
* @param isAddStack 是否入回退栈
* @return {@code null} 失败
*/
public static Fragment replaceFragment(@NonNull Fragment srcFragment,
@NonNull Fragment destFragment,
boolean isAddStack) {
if (srcFragment.getArguments() == null) return null;
int containerId = srcFragment.getArguments().getInt(ARGS_ID);
if (containerId == 0) return null;
return replaceFragment(srcFragment.getFragmentManager(), containerId, destFragment, isAddStack);
}
/**
* 替换fragment
*
* @param fragmentManager fragment管理器
* @param containerId 布局Id
* @param fragment fragment
* @param isAddStack 是否入回退栈
* @return fragment
*/
public static Fragment replaceFragment(@NonNull FragmentManager fragmentManager,
int containerId,
@NonNull Fragment fragment,
boolean isAddStack) {
putArgs(fragment, new Args(containerId, false, isAddStack));
return operateFragment(fragmentManager, null, fragment, TYPE_REPLACE_FRAGMENT);
}
/**
* 出栈fragment
*
* @param fragmentManager fragment管理器
* @return {@code true}: 出栈成功<br>{@code false}: 出栈失败
*/
public static boolean popFragment(@NonNull FragmentManager fragmentManager) {
return fragmentManager.popBackStackImmediate();
}
/**
* 出栈到指定fragment
*
* @param fragmentManager fragment管理器
* @param fragmentClass Fragment类
* @param isIncludeSelf 是否包括Fragment类自己
* @return {@code true}: 出栈成功<br>{@code false}: 出栈失败
*/
public static boolean popToFragment(@NonNull FragmentManager fragmentManager,
Class<? extends Fragment> fragmentClass,
boolean isIncludeSelf) {
return fragmentManager.popBackStackImmediate(fragmentClass.getName(), isIncludeSelf ? FragmentManager.POP_BACK_STACK_INCLUSIVE : 0);
}
/**
* 先出栈后新增fragment
*
* @param fragmentManager fragment管理器
* @param containerId 布局Id
* @param fragment fragment
* @param isAddStack 是否入回退栈
* @return fragment
*/
public static Fragment popAddFragment