ViewPager(2)FragmentStatePagerAdapter示例
FragmentStatePagerAdapter适用于数量比较大的viewPager,只会存在 前1个fragment 当前fragment 和下1个fragment,其他销毁,适合加载多数据。本例中有100个pager .
1.代码
1.1 StateViewPagerMain.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 /** 11 * A simple {@link Fragment} subclass. 12 */ 13 public class StateViewPagerMain extends Fragment { 14 15 //1,在layout.xml中添加 ViewPagerFragment,它可以是顶级布局,如下: 16 /* 17 <?xml version="1.0" encoding="utf-8"?> 18 <android.support.v4.view.ViewPagerFragment 19 android:id="@+id/state_view_pager" 20 xmlns:android="http://schemas.android.com/apk/res/android" 21 android:layout_width="match_parent" 22 android:layout_height="match_parent"/> 23 */ 24 25 //2,准备变量,ViewPager和PagerAdapter(本例中是FragmentStatePagerAdapter) 26 ViewPager statePager; 27 StateViewPagerAdapter pagerAdapter; 28 29 30 //3,初始化view pager及 adapter 31 void initViewPager(View v){ 32 //初始化pager 33 statePager = (ViewPager) v.findViewById(R.id.state_view_pager); 34 35 //构造PageAdapter 36 pagerAdapter = new StateViewPagerAdapter(getFragmentManager()); 37 statePager.setAdapter(pagerAdapter); 38 39 //设置page 切换的监听器 40 statePager.addOnPageChangeListener(pageChangeListener); 41 42 //设置pager切换时的动画 43 statePager.setPageTransformer(true, new ZoomOutPageTransformer()); 44 } 45 46 //4,处理page的切换事件 47 ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() { 48 @Override 49 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 50 System.out.println("onPageScrolled " + position); 51 System.out.println("positionOffset " + positionOffset); 52 System.out.println("positionOffsetPixels " + positionOffsetPixels); 53 } 54 55 @Override 56 public void onPageSelected(int position) { 57 System.out.println("onPageSelected " + position); 58 } 59 60 @Override 61 public void onPageScrollStateChanged(int state) { 62 System.out.println("onPageScrollStateChanged " + state); 63 } 64 }; 65 66 //5,pager之间动画切换类 67 public class ZoomOutPageTransformer implements ViewPager.PageTransformer { 68 private static final float MIN_SCALE = 0.85f; 69 private static final float MIN_ALPHA = 0.5f; 70 71 public void transformPage(View view, float position) { 72 int pageWidth = view.getWidth(); 73 int pageHeight = view.getHeight(); 74 75 if (position < -1) { // [-Infinity,-1) 76 // This page is way off-screen to the left. 77 view.setAlpha(0); 78 79 } else if (position <= 1) { // [-1,1] 80 // Modify the default slide transition to shrink the page as well 81 float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position)); 82 float vertMargin = pageHeight * (1 - scaleFactor) / 2; 83 float horzMargin = pageWidth * (1 - scaleFactor) / 2; 84 if (position < 0) { 85 view.setTranslationX(horzMargin - vertMargin / 2); 86 } else { 87 view.setTranslationX(-horzMargin + vertMargin / 2); 88 } 89 90 // Scale the page down (between MIN_SCALE and 1) 91 view.setScaleX(scaleFactor); 92 view.setScaleY(scaleFactor); 93 94 // Fade the page relative to its size. 95 view.setAlpha(MIN_ALPHA + 96 (scaleFactor - MIN_SCALE) / 97 (1 - MIN_SCALE) * (1 - MIN_ALPHA)); 98 99 } else { // (1,+Infinity] 100 // This page is way off-screen to the right. 101 view.setAlpha(0); 102 } 103 } 104 } 105 @Override 106 public View onCreateView(LayoutInflater inflater, ViewGroup container, 107 Bundle savedInstanceState) { 108 // Inflate the layout for this fragment 109 View v = inflater.inflate(R.layout.fragment_state_pager_main, container, false); 110 111 initViewPager(v); 112 113 return v; 114 } 115 }
1.2 StateViewPagerAdapter.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.FragmentStatePagerAdapter; 5 6 public class StateViewPagerAdapter extends FragmentStatePagerAdapter { 7 8 9 /* 10 FragmentStatePagerAdapter适用于数量比较大的viewPager, 11 只会存在 前1个fragment 当前fragment 和下1个fragment,其他销毁,适合加载多数据。 12 */ 13 public StateViewPagerAdapter(FragmentManager fm) { 14 super(fm); 15 } 16 17 @Override 18 public Fragment getItem(int position) { 19 System.out.println("Fragment " + position + " created"); 20 StateViewPagerFragment fragment = new StateViewPagerFragment(); 21 Bundle args = new Bundle(); 22 args.putInt("ID",position); 23 24 fragment.setArguments(args); 25 return fragment; 26 } 27 28 @Override 29 public int getCount() { 30 return 100; 31 } 32 }
1.3 StateViewPagerFragment.java
1 import android.graphics.Color; 2 import android.os.Bundle; 3 import android.support.v4.app.Fragment; 4 import android.view.LayoutInflater; 5 import android.view.View; 6 import android.view.ViewGroup; 7 import android.widget.TextView; 8 9 import com.txw.e.viewpager.R; 10 11 public class StateViewPagerFragment extends Fragment { 12 13 14 @Override 15 public View onCreateView(LayoutInflater inflater, ViewGroup container, 16 Bundle savedInstanceState) { 17 // Inflate the layout for this fragment 18 View v = inflater.inflate(R.layout.fragment_state_pager, container, false); 19 v.setBackgroundColor(Color.parseColor("#FFFFFF")); 20 Bundle args = getArguments(); 21 TextView tv = (TextView) v.findViewById(R.id.state_view_pager_content); 22 tv.setText("Fragment : " + args.getInt("ID")); 23 return v; 24 } 25 26 @Override 27 public void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 System.out.println("StateViewPagerFragment" + getArguments().getInt("ID") + "::onCreate()"); 30 } 31 @Override 32 public void onDestroy() { 33 super.onDestroy(); 34 System.out.println("StateViewPagerFragment" + getArguments().getInt("ID") + "::onDestroy()"); 35 } 36 }
2.xml
2.1 fragment_state_pager_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <android.support.v4.view.ViewPager 3 android:id="@+id/state_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_state_pager.xml
1 <LinearLayout 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:orientation="vertical" 6 tools:context=".StateViewPager.StateViewPagerFragment"> 7 8 <TextView 9 android:id="@+id/state_view_pager_txt" 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:text=" FragmentStatePagerAdapter适用于数量比较大的viewPager,\n 只会存在 前1个fragment 当前fragment 和下1个fragment,其他销毁,适合加载多数据。本例中有100个pager。\n可以在LogCat中查看page对应的Fragment\n的onCreate,onDestory"/> 13 14 <TextView 15 android:id="@+id/state_view_pager_content" 16 android:layout_width="match_parent" 17 android:layout_height="match_parent" 18 android:gravity="center_horizontal|center_vertical" 19 android:layout_gravity="center_horizontal|center_vertical" 20 android:text="@string/hello_blank_fragment"/> 21 22 </LinearLayout>