There is a DrawerLayout . I select the menu item on it and a fragment opens. I need a transition (animation). Here is the fragment code:

 public class RulesFragment extends Fragment { private View myView; private static final String TAG = "myLogs"; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); getActivity().overridePendingTransition(R.anim.trans_left_in, R.anim.trans_left_out); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { myView = inflater.inflate(R.layout.fragment_rules, container, false); Log.d(TAG, "RulesFragment"); getActivity().setTitle(R.string.nav_rules); return myView; } 

}

Here is the fragment markup:

 <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="@string/rules_title_text" android:textColor="?attr/editTextColor" android:textSize="@dimen/size_textview_phrase" android:textStyle="bold" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/rules_text" android:textColor="?attr/editTextColor" android:textSize="@dimen/size_textview_phrase" android:textStyle="normal" /> </LinearLayout> </android.support.v4.widget.NestedScrollView> 

Please suggest what the problem is. Thank you in advance!

  • Do you want to animate a fragment when you click in the sidebar? - Flippy
  • Yes, when I open the side menu and select a fragment, I want it to “pop out”. - Maxim Fomichev
  • 2
  • Thanks, the first version of fTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); it seems to work (although not very noticeably), but the second option does not pass fTransaction.setCustomAnimations(R.animator.slide_in_left, R.animator.slide_in_right); underlines in red. Although in any case they pushed me to search, according to other criteria. So your answer needs to be made an “answer”) - Maxim Fomichev
  • This topic is many trodden, a lot of information, good luck! The answer is given - Flippy

1 answer 1

There is a wonderful article - fragments animation - http://developer.alexanderklimov.ru/android/animation/fragmentsanimation.php .

 FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right); ft.replace(R.id.fragment_container, newFragment, "fragment"); ft.commit(); 

If you use Fragment from the support library, then you should use this kind of xml -

 <?xml version="1.0" encoding="utf-8"?> <set> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:propertyName="x" android:valueType="floatType" android:valueFrom="-1280" android:valueTo="0" android:duration="500"/> </set> 
  • one
    Although the link can find the answer to the question, it is better to point out the most important thing here, and give the link as a source. If the page to which the link leads will be changed, the response link may become invalid. - From the queue of checks - αλεχολυτ
  • Yes, I just put off adding code. Everything corrected - Flippy
  • one
    and who is Samvra? In your profile - αλεχολυτ
  • @alexolut, this is a typo. Samara city. I just can not change, I do not know how, I always sit through the phone. Yes, and I forget) - Flippy