Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apply PageTransformer to PagerView as soon as possible

I have a PageTransfomer applied to a ViewPager, it works great but I want to launch the page's transformation as soon as I set the PageTransformer to the ViewPager. I already tried:

  • setCurrentItem(int)
  • setCurrentItem(int, true)
  • beginFakeDrag(), fakeDragBy(float), endFakeDrag()
  • invalidate()
  • getAdapter().notifyDataSetChanged()

I've tried all these in the onCreate of my activity but maybe I'm wrong.

Does anyone have a clue ?

Thanks

like image 794
magiccyril Avatar asked Jan 11 '13 16:01

magiccyril


People also ask

How many methods does the PageTransformer of a ViewPager expose?

PageTransformer interface and supply it to the view pager. The interface exposes a single method, transformPage() . At each point in the screen's transition, this method is called once for each visible page (generally there's only one visible page) and for adjacent pages just off the screen.

How does ViewPager2 work?

So a Viewpager is an android widget that is used to navigate from one page to another page by swiping left or right using the same activity. So when we use Viewpager, we can add different layouts in one activity and this can be done by using the fragments.

What is ViewPager2 in Android?

ViewPager2 is built on RecyclerView , which means it has access to the DiffUtil utility class. This results in several benefits, but most notably it means that ViewPager2 objects natively take advantage of the dataset change animations from the RecyclerView class.


2 Answers

Based on Oleg's answer is used the code below for my app.

My addition is to check the returned result of beginFakeDrag() inside _invalidatePageTransformer.

I call sendInvalidatePageTransformer() from inside

  • onConfigurationChanged() when the orientation changed
  • inside the LoaderCallback<Cursor>-methods in my Fragment

    private Handler handler = new Handler()
    {
            public void handleMessage(Message msg)
            {
                    switch(msg.what)
                    {
                            case 0:
                                    _invalidatePageTransformer();
                                    break;
                    }
            }
    };
    
    private void _invalidatePageTransformer()
    {
            //no need to invalidate if we have no adapter or no items
            if (this.getAdapter() != null && this.getAdapter().getCount() > 0)
            {
                    //import check here, only fakeDrag if "beginFakeDrag()" returns true
                    if (this.beginFakeDrag())
                    {
                            this.fakeDragBy(0f);
                            this.endFakeDrag();
                    }
            }
    }
    
    public void sendInvalidatePageTransformer()
    {
            this.handler.sendEmptyMessage(0);
    }
    

EDIT: Note: This code is located inside a custom ViewPager-subclass

like image 112
Jelle Avatar answered Nov 15 '22 02:11

Jelle


Try one of these things:

  1. Put the code in onPostCreate()
  2. Put the code in a handler; i.e. new Handler().post(new Runnable() { /* your code */ });
like image 45
Oleg Vaskevich Avatar answered Nov 15 '22 02:11

Oleg Vaskevich