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:
I've tried all these in the onCreate of my activity but maybe I'm wrong.
Does anyone have a clue ?
Thanks
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.
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.
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.
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 changedinside 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
Try one of these things:
onPostCreate()
new Handler().post(new Runnable() { /* your code */ });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With