1. 程式人生 > >android viewpag加fragment簡單應用

android viewpag加fragment簡單應用

 ViewPager viewPager=(ViewPager)findViewById(R.id.viewpager);
 List<Fragment> list=new ArrayList<Fragment>();
 list.add(new ClassificationFragment());
 list.add(new FinequalityFragment());
 list.add(new RecommendFragment());
 list.add(new LivebroadcastFragment());
 list.add(new AnchorFragment());
 ViewPagerFragmentAdapter adapter=new ViewPagerFragmentAdapter( (),ligetSupportFragmentManagerst);
 viewPager.setAdapter(adapter);
 //viewPager.setPageMargin(100);
 //viewPager.setOffscreenPageLimit(2);
// viewPager.setCurrentItem(3);
 viewPager.setPageTransformer(false,new DepthPageTransformer() );
public class ClassificationFragment extends Fragment {

    View mView;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mView == null) {
                     mView = inflater.inflate(R.layout.fragment_layout,null);
              }
        ((ImageView)mView.findViewById(R.id.im_people)).setImageResource(R.drawable.Classification);
            return mView;
         }

}
public class ViewPagerFragmentAdapter extends FragmentPagerAdapter {
    private List<Fragment> mList = new ArrayList<Fragment>();
    String[] mTitle=new String[]{"分類","精品","推薦","直播","主播"};
    public ViewPagerFragmentAdapter(FragmentManager fm, List<Fragment> list) {
        super(fm);
        this.mList = list;
    }

    @Override
    public Fragment getItem(int position) {
        return mList.get(position);
    }

    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return mTitle[position];
      //  return super.getPageTitle(position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
     super.destroyItem(container, position, object);
}
   @Override
   public Object instantiateItem(ViewGroup container, int position) {
    return super.instantiateItem(container, position);
}
}
//官方推薦的切換頁面效果
public class DepthPageTransformer implements ViewPager.PageTransformer {
    private static final float MIN_SCALE=0.75f;

    @Override
    public void transformPage(@NonNull View page, float position) {
        int pageWidth=page.getWidth();
        if (position < -1) {
            page.setAlpha(0);
            Log.d("DepthPageTransformer","position <=-1");
        } else if (position <= 0) {
            page.setAlpha(1);
            page.setTranslationX(0);
            page.setScaleX(1);
            page.setScaleY(1);
            Log.d("DepthPageTransformer","position <= 0");
        }else if (position <= 1) { // (0,1]
            //頁面在由中間頁滑動到右側頁面 或者 由右側頁面滑動到中間頁
            // 淡入淡出效果
            page.setAlpha(1 - position);
            // 反方向移動
            page.setTranslationX(pageWidth * -position);
            // 0.75-1比例之間縮放
            float scaleFactor = MIN_SCALE
                    + (1 - MIN_SCALE) * (1 - Math.abs(position));
            page.setScaleX(scaleFactor);
            page.setScaleY(scaleFactor);
            Log.d("DepthPageTransformer","position <= 1");
        } else { // (1,+Infinity]
            // 頁面遠離右側頁面
            page.setAlpha(0);
            Log.d("DepthPageTransformer","position >= 1");
        }


    }
}