Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue using ViewPager and PageTransformer with Android API <11

I need to write a custom transition for a ViewPager on an app with android:minSdkVersion="9".

I'm trying to run the ZoomOutPageTransformer given here.

I get no compiling error, and on a Samsung Galaxy S running Android 2.3.3 (API 10), the line

pager.setPageTransformer(true, new ZoomOutPageTransformer());

does not give any error, but the code in ZoomOutPageTransformer's transformPage is never executed.

The code works perfectly if I set android:minSdkVersion="11" and use my Nexus 4 (API 17).

Can these PageTransformers be used with API 9 and 10? Otherwise, how can I create custom transitions for ViewPager?

EDIT

Here's the code I use:

public class HomeFragment extends Fragment{ 

    ViewPager mNewsTitlePager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.home_content, container, false);
    }

    @Override
    public void onActivityCreated (Bundle savedInstanceState){

        super.onActivityCreated(savedInstanceState); 

        mNewsTitlePager = (ViewPager) getView().findViewById(R.id.news_title_pager);
        mNewsTitlePager.setPageTransformer(true, new ZoomOutPageTransformer());

        mNewsTitlePager.setAdapter(new NewsPagerAdapter(news_item_list));


    }

    private class NewsPagerAdapter extends PagerAdapter {

        private List<News> news_item_list;
        private LayoutInflater inflater;

        NewsPagerAdapter(List<News> news_item_list) {
            this.news_item_list = news_item_list;
            inflater = getActivity().getLayoutInflater();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView((View) object);
        }

        @Override
        public void finishUpdate(View container) {
        }

        @Override
        public int getCount() {
            return this.news_item_list.size();
        }

        @Override
        public Object instantiateItem(ViewGroup view, int position) {
            TextView news_title = (TextView) inflater.inflate(R.layout.news_title_item, view, false);

            news_title.setText(news_item_list.get(position).title);

            ((ViewPager) view).addView(news_title, 0);
            return news_title;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view.equals(object);
        }

        @Override
        public void restoreState(Parcelable state, ClassLoader loader) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View container) {
        }


    }

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

        public void transformPage(View view, float position) {
            int pageWidth = view.getWidth();
            int pageHeight = view.getHeight();

            if (position < -1) { // [-Infinity,-1)
                // This page is way off-screen to the left.
                view.setAlpha(0);

            } else if (position <= 1) { // [-1,1]
                // Modify the default slide transition to shrink the page as well
                float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
                float vertMargin = pageHeight * (1 - scaleFactor) / 2;
                float horzMargin = pageWidth * (1 - scaleFactor) / 2;
                if (position < 0) {
                    view.setTranslationX(horzMargin - vertMargin / 2);
                } else {
                    view.setTranslationX(-horzMargin + vertMargin / 2);
                }

                // Scale the page down (between MIN_SCALE and 1)
                view.setScaleX(scaleFactor);
                view.setScaleY(scaleFactor);

                // Fade the page relative to its size.
                view.setAlpha(MIN_ALPHA +
                        (scaleFactor - MIN_SCALE) /
                        (1 - MIN_SCALE) * (1 - MIN_ALPHA));

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

    }


}
like image 784
jul Avatar asked Apr 15 '13 15:04

jul


2 Answers

I copied the ViewPager class from here and removed the if statement for the version and called it TransformableViewPager

public void setPageTransformer(boolean reverseDrawingOrder, PageTransformer transformer) {
    if (Build.VERSION.SDK_INT >= 11) {
        final boolean hasTransformer = transformer != null;
        final boolean needsPopulate = hasTransformer != (mPageTransformer != null);
        mPageTransformer = transformer;
        setChildrenDrawingOrderEnabledCompat(hasTransformer);
        if (hasTransformer) {
            mDrawingOrder = reverseDrawingOrder ? DRAW_ORDER_REVERSE : DRAW_ORDER_FORWARD;
        } else {
            mDrawingOrder = DRAW_ORDER_DEFAULT;
        }
        if (needsPopulate) populate();
    }
}

I also had to change all PageTransformer to ViewPager.PageTransformer. Then did the transformations in ZoomOutPageTranformer like this,

if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB){
    // > 11 version
    view.setAlpha(0);
}
else
{
    // Nine Old Androids version
    ViewHelper.setAlpha(view, 0);
 }

This worked even for 2.2

like image 88
hellcast Avatar answered Oct 11 '22 13:10

hellcast


Ok: I got the answer at the beginning of the doc:

As property animation is only supported as of Android 3.0 and forward, setting a PageTransformer on a ViewPager on earlier platform versions will be ignored.

I'll use ViewFlipper instead.

like image 40
jul Avatar answered Oct 11 '22 14:10

jul