Apply PageTransformer to PagerView as soon as possible

I have a PageTransfomer application applied to the ViewPager, it works fine, but I want to start the page conversion as soon as I installed PageTransformer in the ViewPager. I already tried:

  • setCurrentItem (integer)
  • setCurrentItem (int, true)
  • beginFakeDrag (), fakeDragBy (float), endFakeDrag ()
  • Invalidate ()
  • getAdapter (). NotifyDataSetChanged ()

I tried all this in onCreate of my activity, but maybe I'm wrong.

Does anyone have a clue?

thanks

+5
source share
2 answers

Try one of the following things:

  • Put the code in onPostCreate()
  • Put the code in the handler; those. new Handler().post(new Runnable() { /* your code */ });
+2
source

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

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

I call sendInvalidatePageTransformer() from the inside

  • onConfigurationChanged() when changing orientation
  • inside 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 inside a custom subclass of ViewPager

+4
source

Source: https://habr.com/ru/post/947463/


All Articles