build.gradle (Module: app)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
apply plugin: 'com.android.application' android { compileSdkVersion 26 defaultConfig { applicationId "com.phaisarn.myapplication" minSdkVersion 21 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.android.support:support-v4:26.1.0' implementation 'com.labo.kaji:fragmentanimations:0.1.1' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' } |
fragment1.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#cde"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Page 1" android:textSize="30sp" android:layout_gravity="center"/> </FrameLayout> |
fragment2.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ecd"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Page 2" android:textSize="30sp" /> </FrameLayout> |
fragment3.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#dec"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Page 3" android:textSize="30sp" /> </FrameLayout> |
Fragment1.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package com.phaisarn.myapplication; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; import com.labo.kaji.fragmentanimations.FlipAnimation; public class Fragment1 extends Fragment { public Fragment1() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment1, container, false); } @Override public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) { /* รูปแบบที่มีให้เลือก CubeAnimation FlipAnimation MoveAnimation PushPullAnimation SidesAnimation */ return FlipAnimation.create(FlipAnimation.LEFT, enter, 500); //return PushPullAnimation.create(PushPullAnimation.LEFT, enter, 500); //return CubeAnimation.create(CubeAnimation.LEFT, enter, 500); //return SidesAnimation.create(SidesAnimation.LEFT, enter, 500); //return MoveAnimation.create(MoveAnimation.LEFT, enter, 500); /* ถ้าต้องการใช้ Transform คนละแบบระหว่าง enter กับ exit if (enter) { return MoveAnimation.create(MoveAnimation.UP, enter, 500); } else { return CubeAnimation.create(CubeAnimation.UP, enter, 500); } */ } } |
Fragment2.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package com.phaisarn.myapplication; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; import com.labo.kaji.fragmentanimations.FlipAnimation; public class Fragment2 extends Fragment { public Fragment2() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment2, container, false); } @Override public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) { /* รูปแบบที่มีให้เลือก CubeAnimation FlipAnimation MoveAnimation PushPullAnimation SidesAnimation */ return FlipAnimation.create(FlipAnimation.LEFT, enter, 500); //return PushPullAnimation.create(PushPullAnimation.LEFT, enter, 500); //return CubeAnimation.create(CubeAnimation.LEFT, enter, 500); //return SidesAnimation.create(SidesAnimation.LEFT, enter, 500); //return MoveAnimation.create(MoveAnimation.LEFT, enter, 500); /* ถ้าต้องการใช้ Transform คนละแบบระหว่าง enter กับ exit if (enter) { return MoveAnimation.create(MoveAnimation.UP, enter, 500); } else { return CubeAnimation.create(CubeAnimation.UP, enter, 500); } */ } } |
Fragment3.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package com.phaisarn.myapplication; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; import com.labo.kaji.fragmentanimations.FlipAnimation; public class Fragment3 extends Fragment { public Fragment3() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment3, container, false); } @Override public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) { /* รูปแบบที่มีให้เลือก CubeAnimation FlipAnimation MoveAnimation PushPullAnimation SidesAnimation */ return FlipAnimation.create(FlipAnimation.LEFT, enter, 500); //return PushPullAnimation.create(PushPullAnimation.LEFT, enter, 500); //return CubeAnimation.create(CubeAnimation.LEFT, enter, 500); //return SidesAnimation.create(SidesAnimation.LEFT, enter, 500); //return MoveAnimation.create(MoveAnimation.LEFT, enter, 500); /* ถ้าต้องการใช้ Transform คนละแบบระหว่าง enter กับ exit if (enter) { return MoveAnimation.create(MoveAnimation.UP, enter, 500); } else { return CubeAnimation.create(CubeAnimation.UP, enter, 500); } */ } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity"> <GridLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"> <Button android:id="@+id/button1" android:text="Fragment 1" /> <Button android:id="@+id/button2" android:text="Fragment 2" /> <Button android:id="@+id/button3" android:text="Fragment 3" /> </GridLayout> <FrameLayout android:id="@+id/frameLayout" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
package com.phaisarn.myapplication; import android.support.v4.app.Fragment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Fragment1 f1 = new Fragment1(); Button button1 = findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { replaceFragment(f1); } }); final Fragment2 f2 = new Fragment2(); Button button2 = findViewById(R.id.button2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { replaceFragment(f2); } }); final Fragment3 f3 = new Fragment3(); Button button3 = findViewById(R.id.button3); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { replaceFragment(f3); } }); } private void replaceFragment(Fragment fragment) { getSupportFragmentManager().beginTransaction() .addToBackStack(null) .replace(R.id.frameLayout, fragment) .commit(); } } |