0

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.

Huy Duong Tu
  • 7,798
  • 5
  • 25
  • 46

1 Answers1

1

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;
}
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
owe
  • 4,890
  • 7
  • 36
  • 47
  • Sorry for unclear question. From the sample, I create another PagerAdapter by extending this. And set this adapter for ViewPager. – Huy Duong Tu Jan 13 '14 at 14:22
  • Sorry in this case I can't give you more help...till now I always used `FragmentStatePagerAdapter` (-> with Fragments my UI is much more flexible for Phones and Tablets). I would suggest you to post a more precise question with codesnippets that show what you tried so far (or update this one). Without code or a log file people can just guess what`s the problem. – owe Jan 13 '14 at 15:50
  • I updated my answer! Please try the solution and tell me if it works. – owe Jan 14 '14 at 08:19
  • But I don't know if I set layout params. It doesn't work. Could you explain it? Thanks – Huy Duong Tu Jan 14 '14 at 08:36
  • Sorry, i tested it now and I can't explain this strange behaviour: if you set the `ViewPager.LayoutParams` all Views will be instatiated...but only the last View will be shown... – owe Jan 14 '14 at 09:18
  • yes, it's so strange. Anyway, I can keep going with my project. Thanks for your time. – Huy Duong Tu Jan 14 '14 at 10:43