ViewPager(1)FragmentPagerAdapter
FragmentPagerAdapter 适合只有少量的pager,所有pager同时全部存在,不会有被销毁的,page过多很容易内存溢出.
1,代码
1.1 ViewPagerMain.java
1 import android.os.Bundle; 2 import android.support.v4.app.Fragment; 3 import android.support.v4.view.ViewPager; 4 import android.view.LayoutInflater; 5 import android.view.View; 6 import android.view.ViewGroup; 7 8 import com.txw.e.viewpager.R; 9 10 public class ViewPagerMain extends Fragment { 11 12 //1,在layout.xml中添加 ViewPagerFragment,它可以是顶级布局,如下: 13 /* 14 <?xml version="1.0" encoding="utf-8"?> 15 <android.support.v4.view.ViewPagerFragment 16 android:id="@+id/view_pager" 17 xmlns:android="http://schemas.android.com/apk/res/android" 18 android:layout_width="match_parent" 19 android:layout_height="match_parent"/> 20 */ 21 22 //2,准备变量,ViewPager和 PagerAdapter ,OnPageChangeListener 23 ViewPager pager; 24 ViewPagerAdapter pagerAdapter; 25 ViewPager.SimpleOnPageChangeListener simpleOnPageChangeListener; 26 27 //3,初始化pager 28 void initPager(View v){ 29 //从layout.xml中初始化pager 30 pager = (ViewPager) v.findViewById(R.id.view_pager); 31 32 //初始化page adapter 33 pagerAdapter = new ViewPagerAdapter(getFragmentManager()); 34 35 //设置adapter 36 pager.setAdapter(pagerAdapter); 37 38 //设置page切换监听者 39 simpleOnPageChangeListener = new ViewPager.SimpleOnPageChangeListener(); 40 pager.addOnPageChangeListener(simpleOnPageChangeListener); 41 42 //设置pager切换动画 43 pager.setPageTransformer(true, new DepthPageTransformer()); 44 45 46 } 47 48 //4,处理 pager 切换事件 49 //这里用的ViewPager.SimpleOnPageChangeListener,它什么也不做。 50 51 //5,重写切换动画类,可以把这个类放到外面去。 52 public class DepthPageTransformer implements ViewPager.PageTransformer { 53 private static final float MIN_SCALE = 0.75f; 54 55 public void transformPage(View view, float position) { 56 int pageWidth = view.getWidth(); 57 58 if (position < -1) { // [-Infinity,-1) 59 // This page is way off-screen to the left. 60 view.setAlpha(0); 61 62 } else if (position <= 0) { // [-1,0] 63 // Use the default slide transition when moving to the left page 64 view.setAlpha(1); 65 view.setTranslationX(0); 66 view.setScaleX(1); 67 view.setScaleY(1); 68 69 } else if (position <= 1) { // (0,1] 70 // Fade the page out. 71 view.setAlpha(1 - position); 72 73 // Counteract the default slide transition 74 view.setTranslationX(pageWidth * -position); 75 76 // Scale the page down (between MIN_SCALE and 1) 77 float scaleFactor = MIN_SCALE 78 + (1 - MIN_SCALE) * (1 - Math.abs(position)); 79 view.setScaleX(scaleFactor); 80 view.setScaleY(scaleFactor); 81 82 } else { // (1,+Infinity] 83 // This page is way off-screen to the right. 84 view.setAlpha(0); 85 } 86 } 87 } 88 89 @Override 90 public View onCreateView(LayoutInflater inflater, ViewGroup container, 91 Bundle savedInstanceState) { 92 // Inflate the layout for this fragment 93 View v = inflater.inflate(R.layout.fragment_pager_main, container, false); 94 95 initPager(v); 96 97 return v; 98 } 99 }
1.2 ViewPagerAdapter.java
1 import android.os.Bundle; 2 import android.support.v4.app.Fragment; 3 import android.support.v4.app.FragmentManager; 4 import android.support.v4.app.FragmentPagerAdapter; 5 6 public class ViewPagerAdapter extends FragmentPagerAdapter{ 7 8 /* 9 适合只有少量的pager,所有pager同时全部存在,不会有被销毁的,page过多很容易内存溢出. 10 */ 11 public ViewPagerAdapter(FragmentManager fm) { 12 super(fm); 13 } 14 15 @Override 16 public Fragment getItem(int position) { 17 System.out.println("getItem:create fragment"); 18 ViewPagerFragment fragment = new ViewPagerFragment(); 19 Bundle args = new Bundle(); 20 args.putInt("ID",position); 21 fragment.setArguments(args); 22 return fragment; 23 } 24 25 @Override 26 public int getCount() { 27 return 5; 28 } 29 }
1.3 ViewPagerFragment.java
1 import android.os.Bundle; 2 import android.support.v4.app.Fragment; 3 import android.view.LayoutInflater; 4 import android.view.View; 5 import android.view.ViewGroup; 6 import android.widget.TextView; 7 8 import com.txw.e.viewpager.R; 9 10 public class ViewPagerFragment extends Fragment { 11 12 13 @Override 14 public void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 System.out.println("ViewPagerFragment" + getArguments().getInt("ID") + "::onCreate()"); 17 } 18 19 @Override 20 public View onCreateView(LayoutInflater inflater, ViewGroup container, 21 Bundle savedInstanceState) { 22 // Inflate the layout for this fragment 23 View v = inflater.inflate(R.layout.fragment_pager, container, false); 24 TextView tv = (TextView) v.findViewById(R.id.view_pager_content); 25 Bundle args = getArguments(); 26 int position = args.getInt("ID"); 27 tv.setText("ViewPagerFragment " + position); 28 v.setTranslationX(-1 * v.getWidth() * position); 29 return v; 30 } 31 32 @Override 33 public void onDestroy() { 34 super.onDestroy(); 35 System.out.println("ViewPagerFragment" + getArguments().getInt("ID") + "::onDestroy()"); 36 } 37 38 }
2,xml
2.1 fragment_pager_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <android.support.v4.view.ViewPager 3 android:id="@+id/view_pager" 4 xmlns:android="http://schemas.android.com/apk/res/android" 5 android:background="#FFFFFF" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent"/>
2.2 fragment_pager.xml
1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="#FFFFFF" 6 tools:context=".ViewPager.ViewPagerFragment"> 7 8 <TextView 9 android:id="@+id/view_pager_txt" 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:text="适合只有少量的pager,所有pager同时全部存在,不会有被销毁的,page过多很容易内存溢出.\n可以在LogCat中查看page对应的Fragment\n的onCreate,onDestory"/> 13 14 <TextView 15 android:id="@+id/view_pager_content" 16 android:layout_width="match_parent" 17 android:layout_height="match_parent" 18 android:gravity="center_horizontal|center_vertical" 19 android:text="@string/hello_blank_fragment"/> 20 21 </FrameLayout>