Android - Sliding fragments when click list item

I am trying to implement a rolling animation to go from fragment1 to fragment2, for example this image .

First, I tried to implement xml using set and translate, but I got a RuntimeException "Unknown animator translator name".

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <translate android:fromXDelta="0%p" android:toXDelta="-100%p" android:duration="500"> </translate> </set> 

Secondly, I tried to solve the problem using a class that extends the scope and adds the getXFraction and setXFraction method, for example this post

 public class SlidingFrameLayout extends FrameLayout { private static final String TAG = SlidingFrameLayout.class.getName(); public SlidingFrameLayout(Context context) { super(context); } public SlidingFrameLayout(Context context, AttributeSet attrs) { super(context, attrs); } public float getXFraction() { final int width = getWidth(); if(width != 0) return getX() / getWidth(); else return getX(); } public void setXFraction(float xFraction) { final int width = getWidth(); setX((width > 0) ? (xFraction * width) : -9999); } public float getYFraction() { final int height = getHeight(); if(height != 0) return getY() / getHeight(); else return getY(); } public void setYFraction(float yFraction) { final int height = getHeight(); setY((height > 0) ? (yFraction * height) : -9999); } } 

But I still don’t know. How to use SlidingFrameLayout? Please help me. T ___ T

+4
source share
2 answers

You need to use objectAnimator instead of translation. Here are some posts with examples: Transition between fragments and Snippets and Android animations

+1
source

You should use SlidingFrameLayout as the root view of the fragments that you want to copy in your xml layout file, for example, if your root view was a relative layout:

 <com.company.appname.SlidingFrameLayout 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"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> </RelativeLayout> </com.company.appname.SlidingFrameLayout> 

Then use objectAnimator, for example, to slide in the right direction:

 <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:duration="@android:integer/config_mediumAnimTime" android:interpolator="@android:anim/linear_interpolator" android:propertyName="xFraction" android:valueFrom="0" android:valueTo="1.0"/> 
+1
source

Source: https://habr.com/ru/post/1492950/


All Articles