title: Why doesn't work animation in viewPager method?
body: <p>I have viewPager with 2 views. I need to get the second view by click and by flip. It works. But when I an in the second view I have to have button animation. This button is my customView. This animation doesn't work. And I don't know why. <code>onPageSelected(int position)</code> method works, because I get log.</p>
  <p>my customView class:</p>
  <pre><code>public class CustomView extends LinearLayout {
      private LayoutInflater mInflater;
      private Button button;
      private ViewPager viewPager;
      private float buttonWidth;
  
      public void setPager(ViewPager viewPager) {
          this.viewPager = viewPager;
  
          viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
              @Override
              public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                setClick();
              }
  
              @Override
              public void onPageSelected(int position) {
  
                  ViewTreeObserver vto = button.getViewTreeObserver();
                  vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                      @Override
                      public void onGlobalLayout() {
                          buttonWidth = button.getWidth();
                      }
                  });
  
                  DisplayMetrics metrics = new DisplayMetrics();
                  ((Activity) getContext()).getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
  
                  boolean isDisplayPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
                  float middleScreen = isDisplayPortrait ? metrics.widthPixels / 2 : metrics.heightPixels / 2;
                  float movingDistanceForButton = middleScreen - buttonWidth / 2;
  
                  final TranslateAnimation animation = new TranslateAnimation(0, movingDistanceForButton, 0, 0);
                  animation.setDuration(3000);
                  animation.setFillAfter(true);
                  if (position == 1) {
                      button.setAnimation(animation);
                      Log.e(&quot;position1&quot;, &quot;pos1&quot;);
                  } else {
                      button.getAnimation().cancel();
                  }
              }
  
              @Override
              public void onPageScrollStateChanged(int state) {
  
              }
          });
      }
  
     public CustomView(@NonNull Context context, @Nullable AttributeSet attrs) {
          super(context, attrs);
          mInflater = LayoutInflater.from(context);
  
          View v = mInflater.inflate(R.layout.custom_view, this, true);
          button = v.findViewById(R.id.nextBotton);
   }
  
          public void setClick() {
  
          button.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                 viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
  
              }
          });
      }
  </code></pre>
  <p>and my activity class:</p>
  <pre><code>public class Activity extends AppCompatActivity {
      private ViewPager viewPager;
      private PagerAdapter customPagerAdapter;
      private CustomView customView;
  
  
      protected void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          customPagerAdapter = new PagerAdapter(this);
          viewPager = findViewById(R.id.viewPager);
         customView = findViewById(R.id.myCustomView);
  
          viewPager.setAdapter(customPagerAdapter);
         customView.setPager(viewPager);
  }
  </code></pre>