-->

分類

2016年3月31日 星期四

Android ViewPager


來做個一頁可以有三個page然後換頁用翻的transform
MainActivity
public class MainActivity extends AppCompatActivity implements ScreenSlidePageFragment.OnFragmentInteractionListener{
    private static final int NUM_PAGES = 5;
    private float widthScale = 0.6f;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);
        //mPager.setPageTransformer(false, new ZoomOutPageTransformer());
        mPager.setPageTransformer(false, new FlipPageTransformer());

        windowWidth = width;
        //mPager.setPageMargin(-(int)getResources().getDimension(R.dimen.viewpager_margin));

        //set page margin to be half of (viewpager's width - page's width)
        //page's width should be widthScale * viewPagerWidth
        float viewPagerWidth = getResources().getDimension(R.dimen.viewpager_width);
        mPager.setPageMargin((int) ((1.0f - widthScale) * viewPagerWidth / 2.0f));

        mOnPageChangeListener = new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(position == NUM_PAGES - 1) {
                    //avoid to get stuck at last page
                    mPager.setCurrentItem(NUM_PAGES - 2, true);
                    return;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        };


        mPager.addOnPageChangeListener(mOnPageChangeListener);
    }

    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            final ScreenSlidePageFragment returnFragment;
            if(position % 2 == 0){
                String arg0 = "even";
                String arg1 = "isLast";
                if(position == NUM_PAGES - 1){
                    returnFragment = ScreenSlidePageFragment.newInstance(arg0, arg1);
                } else {
                    returnFragment = ScreenSlidePageFragment.newInstance(arg0, "");
                }

            } else {
                String arg0 = "odd";
                String arg1 = "isLast";
                if(position == NUM_PAGES - 1){
                    returnFragment = ScreenSlidePageFragment.newInstance(arg0, arg1);
                } else {
                    returnFragment = ScreenSlidePageFragment.newInstance(arg0, "");
                }
            }

            //return new ScreenSlidePageFragment();
            return returnFragment;
        }

        @Override
        public int getCount() {
            return NUM_PAGES;
        }


        @Override
        public float getPageWidth(int position) {
            return widthScale;
        }


    }

    public class FlipPageTransformer implements ViewPager.PageTransformer {
        private static final float MIN_SCALE = 0.85f;
        private static final float MIN_ALPHA = 0.5f;

        public void transformPage(View view, float position) {
            int pageWidth = view.getWidth();
            int pageHeight = view.getHeight();
            float pageFullWidth = (float)pageWidth / widthScale;
            //I guess this the page's movement from 1 -> 0 or 0 -> -1 will travel the distance of (pageFullWidth - (float)pageWidth) / 2.0f...
            float movement = (pageFullWidth - (float)pageWidth) / 2.0f;
            //float approx equal threshold
            float threshold = 0.0005f;
            if("isLast".equals(view.getTag())){
                //do not display last page
                view.setAlpha(0);
                return;
            }
            if (position < -1) { // [-Infinity,-1)
                // This page is way off-screen to the left.
                view.setAlpha(0);

            } else if (position <= 1) { // [-1,1]
                view.setAlpha(1);

                //pageFullWidth * -position: move page's left edge to align viewpager's left edge
                //(1.0f + position) * movement: move page back to viewpager's left edge(if -1 < position < 0)
                // or view pager's right edge(if 0 < position < 1), but we still need page margin to help...
                view.setTranslationX(pageFullWidth * -position + (1.0f + position) * movement);

                if(position < -threshold){
                    view.setPivotX(0);
                    view.setRotationY(-position * 90.0f);
                } else if(position > threshold){
                    view.setPivotX(pageWidth);
                    view.setRotationY(-position * 90.0f);
                } else {//position == 0.0f
                    view.setPivotX(0);
                    //make page to be at center of viewpager
                    view.setTranslationX(movement);
                    view.setRotationY(0);
                }



            } else { // (1,+Infinity]
                // This page is way off-screen to the right.
                view.setAlpha(0);
            }
        }
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}

ScreenSlidePageFragment
public class ScreenSlidePageFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    public static final String VIEW_TAG_LAST = "last";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public ScreenSlidePageFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment ScreenSlidePageFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static ScreenSlidePageFragment newInstance(String param1, String param2) {
        ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        //return inflater.inflate(R.layout.fragment_screen_slide_page, container, false);
        View view = inflater.inflate(R.layout.fragment_screen_slide_page, container, false);
        if("even".equals(mParam1)){
            view.setBackgroundColor(getResources().getColor(R.color.primary_purple));
        }
        if("isLast".equals(mParam2)){
            view.setTag("isLast");
        }
        return view;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * * See the Android Training lesson Communicating with Other Fragments for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

res/layout/activity_main.xml

    


res/layout/fragment_screen_slid_page.xml

    



res/values/strings.xml

    AAA
    Hello blank fragment


res/values/colors.xml

    #3F51B5
    #303F9F
    #FF4081
    #52bf90
    #398564
    #FF4081
    #e4e5e5
    #e0ac69
    #c68642

    #673AB7
    #512DA8
    #D1C4E9
    #FF9800
    #212121
    #727272
    #FFFFFF
    #B6B6B6
    #374046


res/values/dimens.xml

    
    16dp
    16dp

    240dp

沒有留言:

張貼留言