apply PageTransformer to PagerView as soon as possible

后端 未结 2 1107
抹茶落季
抹茶落季 2021-01-13 10:56

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 alrea

相关标签:
2条回答
  • 2021-01-13 11:16

    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 */ });
    0 讨论(0)
  • 2021-01-13 11:21

    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

    0 讨论(0)
提交回复
热议问题