Search code examples
androidandroid-animationandroid-pageradapter

setPageTransformer() doesn't work with PagerAdapter


I want to customize animation of ViewPager. So I implements ViewPager.PageTransformer and call setPageTransformer(). It worked well with FragmentStatePagerAdapter or FragmentPagerAdapter. But not PagerAdapter.

When I call setPageTransformer() (even though PageTransformer does nothing), the page in PagerAdapter display once and automatically dismiss (never display again) except the last one. When I remove setPageTransformer(), it works normally.

Can anyone know why? Thanks in advance.
You can use the sample code in this link. I use PagerAdapter instead of FragmentPagerAdapter.

EDIT Here is the code I changed.


Solution

  • I think it doesn't work, because the sample code that you posted use Fragments.

    In the API of PagerAdapter you can read:

    Base class providing the adapter to populate pages inside of a ViewPager. You will most likely want to use a more specific implementation of this, such as FragmentPagerAdapter orFragmentStatePagerAdapter.

    When you implement a PagerAdapter, you must override the following methods at minimum:

    instantiateItem(ViewGroup, int)
    destroyItem(ViewGroup, int, Object)
    getCount()
    isViewFromObject(View, Object)
    

    So you can't just take the example and change FragmentPagerAdapter to PagerAdpapter.

    UPDATE 2014-01-14

    I import your code into an existing project where I show a ViewPager.

    I think I found the mistake! Your Adapter works fine (also the PageTransformater). Please change your method instantiateItem in PagerAdper to this and tell me if this is working for you:

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        // TODO Auto-generated method stub
        View view = LayoutInflater.from(mContext).inflate(R.layout.program_item, null);
        /** 
            // This line cause the strange behaviour
            view.setLayoutParams(mParams);
        **/
        ((ImageView) view.findViewById(R.id.img)).setImageResource(R.drawable.default_program);
        container.addView(view);
        return view;
    }