ลองแล้วรันได้ แต่ไม่ แสดง effect อะไร ??
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.ToxicBakery.viewpager.transforms:view-pager-transforms:1.2.32@aar' 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' } |
spinner_item.xml
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/textview_spinner_item" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#0a0" android:padding="10dp" android:textColor="#fff" android:textSize="18sp"> </TextView> |
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> |
fragment4.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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" tools:context=".Fragment4"> <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/hello_blank_fragment" /> </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 |
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; 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); } } |
Fragment2.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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; 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); } } |
Fragment3.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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; 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); } } |
Fragment4.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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; public class Fragment4 extends Fragment { public Fragment4() { // 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.fragment4, container, false); } } |
MyPagerAdapter.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 |
package com.phaisarn.myapplication; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; class MyPagerAdapter extends FragmentPagerAdapter { public MyPagerAdapter(FragmentManager fragMan) { super(fragMan); } @Override public int getCount() { return 4; } @Override public Fragment getItem(int position) { switch(position) { case 0: return new Fragment1(); case 1: return new Fragment2(); case 2: return new Fragment3(); case 3: return new Fragment4(); default: return null; } } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?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"> <Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:backgroundTint="#0a0" /> <android.support.v4.view.ViewPager android:id="@+id/viewPager" 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
package com.phaisarn.myapplication; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Spinner; import android.widget.Toast; import com.ToxicBakery.viewpager.transforms.AccordionTransformer; import com.ToxicBakery.viewpager.transforms.BackgroundToForegroundTransformer; import com.ToxicBakery.viewpager.transforms.CubeInTransformer; import com.ToxicBakery.viewpager.transforms.CubeOutTransformer; import com.ToxicBakery.viewpager.transforms.DefaultTransformer; import com.ToxicBakery.viewpager.transforms.DepthPageTransformer; import com.ToxicBakery.viewpager.transforms.FlipHorizontalTransformer; import com.ToxicBakery.viewpager.transforms.FlipVerticalTransformer; import com.ToxicBakery.viewpager.transforms.ForegroundToBackgroundTransformer; import com.ToxicBakery.viewpager.transforms.RotateDownTransformer; import com.ToxicBakery.viewpager.transforms.RotateUpTransformer; import com.ToxicBakery.viewpager.transforms.ScaleInOutTransformer; import com.ToxicBakery.viewpager.transforms.StackTransformer; import com.ToxicBakery.viewpager.transforms.TabletTransformer; import com.ToxicBakery.viewpager.transforms.ZoomInTransformer; import com.ToxicBakery.viewpager.transforms.ZoomOutSlideTransformer; import com.ToxicBakery.viewpager.transforms.ZoomOutTranformer; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* รูปแบบที่มีให้เลือก AccordionTransformer BackgroundToForeGroundTransformer CubeInTransformer CubeOutTransformer DefaultTransformer DepthPageTransformer FlipHorizontalTransformer FlipVerticalTransformer ForegroundToBackgroundTransformer RotateUpTransformer RotateDownTransformer ScaleInOutTransformer StackTransformer TabletTransformer ZoomInTransformer ZoomOutTranformer ZoomOutSlideTransformer */ final ViewPager.PageTransformer[] transformers = { new AccordionTransformer(), new BackgroundToForegroundTransformer(), new CubeInTransformer(), new CubeOutTransformer(), new DefaultTransformer(), new DepthPageTransformer(), new FlipHorizontalTransformer(), new FlipVerticalTransformer(), new ForegroundToBackgroundTransformer(), new RotateUpTransformer(), new RotateDownTransformer(), new ScaleInOutTransformer(), new StackTransformer(), new TabletTransformer(), new ZoomInTransformer(), new ZoomOutTranformer(), new ZoomOutSlideTransformer() }; String[] items = new String[transformers.length]; for (int i = 0; i < transformers.length; i++) { items[i] = transformers[i].getClass().getSimpleName(); } final Spinner spinner = (Spinner) findViewById(R.id.spinner); ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.spinner_item, items); adapter.setDropDownViewResource(R.layout.spinner_item); spinner.setAdapter(adapter); MyPagerAdapter pagerAdapter = new MyPagerAdapter(getSupportFragmentManager()); final ViewPager pager = (ViewPager) findViewById(R.id.viewPager); pager.setAdapter(pagerAdapter); pager.setPageTransformer(true, transformers[spinner.getSelectedItemPosition()]); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { pager.setPageTransformer(true, transformers[spinner.getSelectedItemPosition()]); //pager.setPageTransformer(true, new FlipHorizontalTransformer()); Toast.makeText(getApplicationContext(), "XXX", Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } } |