Java Code Examples for android.support.v4.app.FragmentTransaction#addSharedElement()

The following examples show how to use android.support.v4.app.FragmentTransaction#addSharedElement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NavigationUtils.java    From Muzesto with GNU General Public License v3.0 6 votes vote down vote up
@TargetApi(21)
public static void navigateToAlbum(Activity context, long albumID, Pair<View, String> transitionViews) {

    FragmentTransaction transaction = ((AppCompatActivity) context).getSupportFragmentManager().beginTransaction();
    Fragment fragment;

    if (TimberUtils.isLollipop() && transitionViews != null && PreferencesUtility.getInstance(context).getAnimations()) {
        Transition changeImage = TransitionInflater.from(context).inflateTransition(R.transition.image_transform);
        transaction.addSharedElement(transitionViews.first, transitionViews.second);
        fragment = AlbumDetailFragment.newInstance(albumID, true, transitionViews.second);
        fragment.setSharedElementEnterTransition(changeImage);
    } else {
        transaction.setCustomAnimations(R.anim.activity_fade_in,
                R.anim.activity_fade_out, R.anim.activity_fade_in, R.anim.activity_fade_out);
        fragment = AlbumDetailFragment.newInstance(albumID, false, null);
    }
    transaction.hide(((AppCompatActivity) context).getSupportFragmentManager().findFragmentById(R.id.fragment_container));
    transaction.add(R.id.fragment_container, fragment);
    transaction.addToBackStack(null).commit();

}
 
Example 2
Source File: NavigationUtils.java    From Muzesto with GNU General Public License v3.0 6 votes vote down vote up
@TargetApi(21)
public static void navigateToArtist(Activity context, long artistID, Pair<View, String> transitionViews) {

    FragmentTransaction transaction = ((AppCompatActivity) context).getSupportFragmentManager().beginTransaction();
    Fragment fragment;

    if (TimberUtils.isLollipop() && transitionViews != null && PreferencesUtility.getInstance(context).getAnimations()) {
        Transition changeImage = TransitionInflater.from(context).inflateTransition(R.transition.image_transform);
        transaction.addSharedElement(transitionViews.first, transitionViews.second);
        fragment = ArtistDetailFragment.newInstance(artistID, true, transitionViews.second);
        fragment.setSharedElementEnterTransition(changeImage);
    } else {
        transaction.setCustomAnimations(R.anim.activity_fade_in,
                R.anim.activity_fade_out, R.anim.activity_fade_in, R.anim.activity_fade_out);
        fragment = ArtistDetailFragment.newInstance(artistID, false, null);
    }
    transaction.hide(((AppCompatActivity) context).getSupportFragmentManager().findFragmentById(R.id.fragment_container));
    transaction.add(R.id.fragment_container, fragment);
    transaction.addToBackStack(null).commit();

}
 
Example 3
Source File: FragmentUtils.java    From AndroidUtilCode with Apache License 2.0 5 votes vote down vote up
private static void addSharedElement(final FragmentTransaction ft,
                                     final View... views) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        for (View view : views) {
            ft.addSharedElement(view, view.getTransitionName());
        }
    }
}
 
Example 4
Source File: AutoSharedElementCallback.java    From native-navigation with MIT License 5 votes vote down vote up
/**
 * Walks the given view group and adds all view with a set transition name to the fragment
 * transaction.
 */
public static void addSharedElementsToFragmentTransaction(
        FragmentTransaction ft, ViewGroup viewGroup) {
  List<Pair<View, String>> transitionViews = new ArrayList<>();
  ViewUtils.findTransitionViews(viewGroup, transitionViews);

  for (Pair<View, String> tv : transitionViews) {
    ft.addSharedElement(tv.first, tv.second);
  }
}
 
Example 5
Source File: EasyIntroCarouselFragment.java    From EasyIntro with Apache License 2.0 5 votes vote down vote up
@Override
public void withOverlaySlide(Fragment slide, @IdRes int container, FragmentManager fragmentManager, @AnimRes int enter, @AnimRes int exit, @AnimRes int popEnter, @AnimRes int popExit, View sharedElement, String transitionName, boolean addToBackStack) {
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    // add to back stack
    if (addToBackStack) {
        transaction.addToBackStack(null);
    }
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    transaction.setCustomAnimations(enter, exit, popEnter, popExit);
    if (sharedElement != null) {
        transaction.addSharedElement(sharedElement, transitionName);
    }
    transaction.replace(container, slide).commit();
}
 
Example 6
Source File: TabManager.java    From ForPDA with GNU General Public License v3.0 5 votes vote down vote up
public void add(TabFragment tabFragment, View sharedElement, Fragment fragment) {
    if (tabFragment == null)
        return;
    String check = null;
    if (tabFragment.getConfiguration().isAlone()) {
        check = getTagContainClass(tabFragment.getClass());
    }
    if (check != null) {
        select(check);
        return;
    }

    activeTag = TAB_PREFIX.concat(Long.toString(System.currentTimeMillis()));
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    hideTabs(transaction);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        tabFragment.setSharedElementEnterTransition(new DetailsTransition());
        tabFragment.setEnterTransition(new Fade());
        fragment.setExitTransition(new Fade());
        tabFragment.setSharedElementReturnTransition(new DetailsTransition());
    }

    transaction.addSharedElement(sharedElement, "detailsCover");
    transaction.add(R.id.fragments_container, tabFragment, activeTag).commit();
    fragmentManager.executePendingTransactions();
    updateFragmentList();
    activeIndex = existingFragments.indexOf(tabFragment);
    tabListener.onChange();
    tabListener.onAddTab(tabFragment);
}
 
Example 7
Source File: TestFragment.java    From glide-support with The Unlicense 5 votes vote down vote up
private void clicked(ItemAdapter.ItemViewHolder vh) {
	DetailFragment fragment = new DetailFragmentWithPager();
	Bundle args = new Bundle();
	args.putSerializable(DetailFragment.ARG_ITEM, vh.item);
	fragment.setArguments(args);
	@SuppressLint("CommitTransaction")
	FragmentTransaction transaction = getFragmentManager()
			.beginTransaction()
			.replace(android.R.id.content, fragment)
			.addToBackStack("details");

	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
		TransitionInflater inf = TransitionInflater.from(getActivity());
		setExitTransition(inf.inflateTransition(android.R.transition.fade));
		fragment.setEnterTransition(inf.inflateTransition(android.R.transition.fade));
		fragment.setSharedElementEnterTransition(getTrans(inf));
		setSharedElementReturnTransition(inf.inflateTransition(R.transition.github_847));

		args.putString(DetailFragment.ARG_TRANS_NAME_IMAGE, vh.image.getTransitionName());
		transaction.addSharedElement(vh.image, vh.image.getTransitionName());

		args.putString(DetailFragment.ARG_TRANS_NAME_TEXT, vh.text.getTransitionName());
		transaction.addSharedElement(vh.text, vh.text.getTransitionName());
	}

	transaction.commit();
}
 
Example 8
Source File: FragmentUtils.java    From styT with Apache License 2.0 4 votes vote down vote up
/**
 * 操作fragment
 *
 * @param fragmentManager fragment管理器
 * @param srcFragment     源fragment
 * @param destFragment    目标fragment
 * @param type            操作类型
 * @param sharedElements  共享元素
 * @return destFragment
 */
private static Fragment operateFragment(@NonNull final FragmentManager fragmentManager,
                                        final Fragment srcFragment,
                                        @NonNull Fragment destFragment,
                                        final int type,
                                        final SharedElement... sharedElements) {
    if (srcFragment == destFragment) return null;
    if (srcFragment != null && srcFragment.isRemoving()) {
        //LogUtils.e(srcFragment.getClass().getName() + " is isRemoving");
        return null;
    }
    String name = destFragment.getClass().getName();
    Bundle args = destFragment.getArguments();

    FragmentTransaction ft = fragmentManager.beginTransaction();
    if (sharedElements == null || sharedElements.length == 0) {
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    } else {
        for (SharedElement element : sharedElements) {// 添加共享元素动画
            ft.addSharedElement(element.sharedElement, element.name);
        }
    }
    switch (type) {
        case TYPE_HIDE_ADD_FRAGMENT:
            ft.hide(srcFragment);
        case TYPE_ADD_FRAGMENT:
            Fragment fragmentByTag = fragmentManager.findFragmentByTag(name);
            if (fragmentByTag != null) {
                destFragment = fragmentByTag;
            }
            ft.add(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_HIDE)) ft.hide(destFragment);
            if (args.getBoolean(ARGS_IS_ADD_STACK)) ft.addToBackStack(name);
            break;
        case TYPE_REMOVE_FRAGMENT:
            ft.remove(destFragment);
            break;
        case TYPE_REMOVE_TO_FRAGMENT:
            List<Fragment> fragments = getFragments(fragmentManager);
            for (int i = fragments.size() - 1; i >= 0; --i) {
                Fragment fragment = fragments.get(i);
                if (fragment == destFragment) {
                    if (srcFragment != null) ft.remove(fragment);
                    break;
                }
                ft.remove(fragment);
            }
            break;
        case TYPE_REPLACE_FRAGMENT:
            ft.replace(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_ADD_STACK)) ft.addToBackStack(name);
            break;
        case TYPE_POP_ADD_FRAGMENT:
            popFragment(fragmentManager);
            ft.add(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_ADD_STACK)) ft.addToBackStack(name);
            break;
        case TYPE_HIDE_FRAGMENT:
            ft.hide(destFragment);
            break;
        case TYPE_SHOW_FRAGMENT:
            ft.show(destFragment);
            break;
        case TYPE_HIDE_SHOW_FRAGMENT:
            ft.hide(srcFragment).show(destFragment);
            break;
    }
    ft.commitAllowingStateLoss();
    return destFragment;
}
 
Example 9
Source File: FragmentUtils.java    From TikTok with Apache License 2.0 4 votes vote down vote up
/**
     * 操作fragment
     *
     * @param fragmentManager fragment管理器
     * @param srcFragment     源fragment
     * @param destFragment    目标fragment
     * @param type            操作类型
     * @param sharedElements  共享元素
     * @return destFragment
     */
    private static Fragment operateFragment(@NonNull FragmentManager fragmentManager,
                                            Fragment srcFragment,
                                            @NonNull Fragment destFragment,
                                            int type,
                                            SharedElement... sharedElements) {
        if (srcFragment == destFragment)
            return null;
        if (srcFragment != null && srcFragment.isRemoving()) {
//            Log.e(srcFragment.getClass().getSimpleName() + " is isRemoving");
            return null;
        }
        String name = destFragment.getClass().getSimpleName();
        Bundle args = destFragment.getArguments();

        FragmentTransaction ft = fragmentManager.beginTransaction();
        if (sharedElements == null || sharedElements.length == 0) {
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        } else {
            for (SharedElement element : sharedElements) {// 添加共享元素动画
                ft.addSharedElement(element.sharedElement, element.name);
            }
        }
        switch (type) {
            case TYPE_ADD_FRAGMENT:
                if (srcFragment != null)
                    ft.hide(srcFragment);
                if (destFragment.isAdded())
                    break;
                ft.add(args.getInt(ARGS_ID), destFragment, name);
                if (args.getBoolean(ARGS_IS_HIDE))
                    ft.hide(destFragment);
                if (args.getBoolean(ARGS_IS_ADD_STACK))
                    ft.addToBackStack(name);
                break;
            case TYPE_REMOVE_FRAGMENT:
                ft.remove(destFragment);
                break;
            case TYPE_REMOVE_TO_FRAGMENT:
                List<Fragment> fragments = getFragments(fragmentManager);
                for (int i = fragments.size() - 1; i >= 0; --i) {
                    Fragment fragment = fragments.get(i);
                    if (fragment == destFragment) {
                        if (srcFragment != null)
                            ft.remove(fragment);
                        break;
                    }
                    ft.remove(fragment);
                }
                break;
            case TYPE_REPLACE_FRAGMENT:
                ft.replace(args.getInt(ARGS_ID), destFragment, name);
                if (args.getBoolean(ARGS_IS_ADD_STACK))
                    ft.addToBackStack(name);
                break;
            case TYPE_POP_ADD_FRAGMENT:
                popFragment(fragmentManager);
                ft.add(args.getInt(ARGS_ID), destFragment, name);
                if (args.getBoolean(ARGS_IS_ADD_STACK))
                    ft.addToBackStack(name);
                break;
            case TYPE_HIDE_FRAGMENT:
                ft.hide(destFragment);
                break;
            case TYPE_SHOW_FRAGMENT:
                ft.show(destFragment);
                break;
            case TYPE_HIDE_SHOW_FRAGMENT:
                ft.hide(srcFragment).show(destFragment);
                break;
        }
        ft.commitAllowingStateLoss();
        return destFragment;
    }
 
Example 10
Source File: FragmentUtils.java    From Android-UtilCode with Apache License 2.0 4 votes vote down vote up
/**
 * 操作fragment
 *
 * @param fragmentManager fragment管理器
 * @param srcFragment     源fragment
 * @param destFragment    目标fragment
 * @param type            操作类型
 * @param sharedElements  共享元素
 * @return destFragment
 */
private static Fragment operateFragment(@NonNull FragmentManager fragmentManager,
                                        Fragment srcFragment,
                                        @NonNull Fragment destFragment,
                                        int type,
                                        SharedElement... sharedElements) {
    if (srcFragment == destFragment) return null;
    if (srcFragment != null && srcFragment.isRemoving()) {
        LogUtils.e(srcFragment.getClass().getName() + " is isRemoving");
        return null;
    }
    String name = destFragment.getClass().getName();
    Bundle args = destFragment.getArguments();

    FragmentTransaction ft = fragmentManager.beginTransaction();
    if (sharedElements == null || sharedElements.length == 0) {
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    } else {
        for (SharedElement element : sharedElements) {// 添加共享元素动画
            ft.addSharedElement(element.sharedElement, element.name);
        }
    }
    switch (type) {
        case TYPE_ADD_FRAGMENT:
            if (srcFragment != null) ft.hide(srcFragment);
            ft.add(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_HIDE)) ft.hide(destFragment);
            if (args.getBoolean(ARGS_IS_ADD_STACK)) ft.addToBackStack(name);
            break;
        case TYPE_REMOVE_FRAGMENT:
            ft.remove(destFragment);
            break;
        case TYPE_REMOVE_TO_FRAGMENT:
            List<Fragment> fragments = getFragments(fragmentManager);
            for (int i = fragments.size() - 1; i >= 0; --i) {
                Fragment fragment = fragments.get(i);
                if (fragment == destFragment) {
                    if (srcFragment != null) ft.remove(fragment);
                    break;
                }
                ft.remove(fragment);
            }
            break;
        case TYPE_REPLACE_FRAGMENT:
            ft.replace(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_ADD_STACK)) ft.addToBackStack(name);
            break;
        case TYPE_POP_ADD_FRAGMENT:
            popFragment(fragmentManager);
            ft.add(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_ADD_STACK)) ft.addToBackStack(name);
            break;
        case TYPE_HIDE_FRAGMENT:
            ft.hide(destFragment);
            break;
        case TYPE_SHOW_FRAGMENT:
            ft.show(destFragment);
            break;
        case TYPE_HIDE_SHOW_FRAGMENT:
            ft.hide(srcFragment).show(destFragment);
            break;
    }
    ft.commitAllowingStateLoss();
    return destFragment;
}
 
Example 11
Source File: FragmentUtils.java    From MVVMArms with Apache License 2.0 4 votes vote down vote up
/**
 * 操作fragment
 *
 * @param fragmentManager fragment管理器
 * @param srcFragment     源fragment
 * @param destFragment    目标fragment
 * @param type            操作类型
 * @param sharedElements  共享元素
 * @return destFragment
 */
private static Fragment operateFragment(@NonNull FragmentManager fragmentManager,
                                        Fragment srcFragment,
                                        @NonNull Fragment destFragment,
                                        int type,
                                        SharedElement... sharedElements) {
    if (srcFragment == destFragment) {
        return null;
    }
    if (srcFragment != null && srcFragment.isRemoving()) {
        Timber.e(srcFragment.getClass().getSimpleName() + " is isRemoving");
        return null;
    }
    String name = destFragment.getClass().getSimpleName();
    Bundle args = destFragment.getArguments();

    FragmentTransaction ft = fragmentManager.beginTransaction();
    if (sharedElements == null || sharedElements.length == 0) {
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    } else {
        // 添加共享元素动画
        for (SharedElement element : sharedElements) {
            ft.addSharedElement(element.sharedElement, element.name);
        }
    }
    switch (type) {
        case TYPE_ADD_FRAGMENT:
            if (srcFragment != null) {
                ft.hide(srcFragment);
            }
            if (destFragment.isAdded()) {
                break;
            }
            ft.add(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_HIDE)) {
                ft.hide(destFragment);
            }
            if (args.getBoolean(ARGS_IS_ADD_STACK)) {
                ft.addToBackStack(name);
            }
            break;
        case TYPE_REMOVE_FRAGMENT:
            ft.remove(destFragment);
            break;
        case TYPE_REMOVE_TO_FRAGMENT:
            List<Fragment> fragments = getFragments(fragmentManager);
            for (int i = fragments.size() - 1; i >= 0; --i) {
                Fragment fragment = fragments.get(i);
                if (fragment == destFragment) {
                    if (srcFragment != null) {
                        ft.remove(fragment);
                    }
                    break;
                }
                ft.remove(fragment);
            }
            break;
        case TYPE_REPLACE_FRAGMENT:
            ft.replace(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_ADD_STACK)) {
                ft.addToBackStack(name);
            }
            break;
        case TYPE_POP_ADD_FRAGMENT:
            popFragment(fragmentManager);
            ft.add(args.getInt(ARGS_ID), destFragment, name);
            if (args.getBoolean(ARGS_IS_ADD_STACK)) {
                ft.addToBackStack(name);
            }
            break;
        case TYPE_HIDE_FRAGMENT:
            ft.hide(destFragment);
            break;
        case TYPE_SHOW_FRAGMENT:
            ft.show(destFragment);
            break;
        case TYPE_HIDE_SHOW_FRAGMENT:
            ft.hide(srcFragment).show(destFragment);
            break;
        default:
            break;
    }
    ft.commitAllowingStateLoss();
    return destFragment;
}
 
Example 12
Source File: FragmentStackManager.java    From Anecdote with Apache License 2.0 4 votes vote down vote up
/**
 * Change tu current displayed fragment by a new one.
 *
 * @param frag            the new fragment to display
 * @param saveInBackstack if we want the fragment to be in backstack
 * @param animate         if we want a nice animation or not
 * @param sharedView      the shared view for the transition
 * @param sharedName      the shared name of the transition
 */
public void changeFragment(
                            AppCompatActivity activity,
                            Fragment frag,
                            boolean saveInBackstack,
                            boolean animate,
                            @Nullable View sharedView,
                            @Nullable String sharedName) {
    String log = "changeFragment: ";
    String backStateName = ((Object) frag).getClass().getName();

    if (frag instanceof AnecdoteFragment) {
        backStateName += frag.getArguments().getString(AnecdoteFragment.ARGS_WEBSITE_PAGE_SLUG);
    }

    try {
        android.support.v4.app.FragmentManager manager = activity.getSupportFragmentManager();
        boolean fragmentPopped = manager.popBackStackImmediate(backStateName, 0);

        if (!fragmentPopped && manager.findFragmentByTag(backStateName) == null) { //fragment not in back stack, create it.
            FragmentTransaction transaction = manager.beginTransaction();

            if (animate) {
                log += " animate";
                transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right);
            }
            if (sharedView != null && !TextUtils.isEmpty(sharedName)) {
                ViewCompat.setTransitionName(sharedView, sharedName);
                transaction.addSharedElement(sharedView, sharedName);
            }

            transaction.replace(R.id.fragment_container, frag, backStateName);

            if (saveInBackstack) {
                log += " addToBackTack(" + backStateName + ")";
                transaction.addToBackStack(backStateName);
            } else {
                log += " NO addToBackTack(" + backStateName + ")";
            }

            transaction.commit();

            // If some snackbar is display, hide it
            if (mSnackbar != null && mSnackbar.isShownOrQueued()) {
                mSnackbar.dismiss();
            }

        } else if (!fragmentPopped && manager.findFragmentByTag(backStateName) != null) {
            log += " fragment not popped but finded: " + backStateName;
        } else {
            log += " nothing to do : " + backStateName + " fragmentPopped: " + fragmentPopped;
            // custom effect if fragment is already instanciated
        }
        Log.d(TAG, log);
    } catch (IllegalStateException exception) {
        Log.w(TAG, "Unable to commit fragment, could be activity as been killed in background. " + exception.toString());
    }
}