使用ViewPager和PageTransformer与Android API< 11

时间:2013-04-15 15:07:39

标签: android android-viewpager

我需要在android:minSdkVersion="9"的应用上为ViewPager编写自定义转换。

我正在尝试运行给定hereZoomOutPageTransformer

我没有编译错误,在运行Android 2.3.3(API 10)的三星Galaxy S上,该行

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

不会出现任何错误,但ZoomOutPageTransformer的transformPage中的代码永远不会执行。

如果我设置android:minSdkVersion="11"并使用我的Nexus 4(API 17),则代码可以正常运行。

这些PageTransformers可以与API 9和10一起使用吗?否则,如何为ViewPager创建自定义转换?

修改

这是我使用的代码:

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);
            }
        }

    }


}

3 个答案:

答案 0 :(得分:10)

我从here复制了ViewPager类并删除了该版本的if语句并将其命名为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();
    }
}

我还必须将所有PageTransformer更改为ViewPager.PageTransformer。然后在ZoomOutPageTranformer中进行了这样的转换,

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

这甚至适用于2.2

答案 1 :(得分:2)

好的:我在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.

我会改用ViewFlipper

答案 2 :(得分:2)

即使它们有效,上述答案都不是正确的答案, 这是我的,不要忘记将nineoldandroids.jar添加到你的libs文件夹中:

 public class ZoomOutPageTransformer 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();

        if (position < -1) {
            ViewHelper.setAlpha(view, 0);
        } else if (position <= 1) { // [-1,1]
            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) {
                ViewHelper.setTranslationX(view,horzMargin - vertMargin / 2);
            } else {
                ViewHelper.setTranslationX(view,-horzMargin + vertMargin / 2);
            }
            ViewHelper.setScaleX(view,scaleFactor);
            ViewHelper.setScaleY(view, scaleFactor);
            ViewHelper.setAlpha(view, MIN_ALPHA + (scaleFactor - MIN_SCALE) / (1 - MIN_SCALE) * (1 - MIN_ALPHA));
        } else {
            ViewHelper.setAlpha(view,0);
        }
    }
}