Java Code Examples for androidx.core.view.ViewCompat#postOnAnimation()

The following examples show how to use androidx.core.view.ViewCompat#postOnAnimation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SmoothRefreshLayout.java    From SmoothRefreshLayout with MIT License 6 votes vote down vote up
void startBounce(int to, int duration) {
    mMode = Constants.SCROLLER_MODE_FLING;
    setInterpolator(SPRING_INTERPOLATOR);
    mLastStart = mIndicator.getCurrentPos();
    mLastTo = to;
    if (sDebug) {
        Log.d(
                TAG,
                String.format(
                        "ScrollChecker: startBounce(): to: %d, duration: %d",
                        to, duration));
    }
    int distance = (int) (mLastTo - mLastStart);
    mLastY = 0;
    mDuration = duration;
    mIsScrolling = true;
    mScroller.startScroll(0, 0, 0, distance, duration);
    removeCallbacks(this);
    ViewCompat.postOnAnimation(SmoothRefreshLayout.this, this);
}
 
Example 2
Source File: RecyclerBinder.java    From litho with Apache License 2.0 6 votes vote down vote up
@UiThread
public void notifyItemRenderCompleteAt(int position, final long timestampMillis) {
  final ComponentTreeHolder holder = mComponentTreeHolders.get(position);
  final EventHandler<RenderCompleteEvent> renderCompleteEventHandler =
      holder.getRenderInfo().getRenderCompleteEventHandler();
  if (renderCompleteEventHandler == null) {
    return;
  }

  final @RenderState int state = holder.getRenderState();
  if (state != ComponentTreeHolder.RENDER_UNINITIALIZED) {
    return;
  }

  // Dispatch a RenderCompleteEvent asynchronously.
  ViewCompat.postOnAnimation(
      mMountedView,
      new RenderCompleteRunnable(
          renderCompleteEventHandler,
          RenderCompleteEvent.RenderState.RENDER_DRAWN,
          timestampMillis));

  // Update the state to prevent dispatch an event again for the same holder.
  holder.setRenderState(ComponentTreeHolder.RENDER_DRAWN);
}
 
Example 3
Source File: BottomSheetBehaviorGoogleMapsLike.java    From CustomBottomSheetBehavior with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    if ( mViewDragHelper != null  &&  mViewDragHelper.continueSettling( true ) ) {
        ViewCompat.postOnAnimation( mView, this );
    } else {
        setStateInternal( mTargetState );
    }
}
 
Example 4
Source File: HeaderBehavior.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
  if (layout != null && scroller != null) {
    if (scroller.computeScrollOffset()) {
      setHeaderTopBottomOffset(parent, layout, scroller.getCurrY());
      // Post ourselves so that we run on the next animation
      ViewCompat.postOnAnimation(layout, this);
    } else {
      onFlingFinished(parent, layout);
    }
  }
}
 
Example 5
Source File: CustomViewPager.java    From AsteroidOSSync with GNU General Public License v3.0 5 votes vote down vote up
private void completeScroll(boolean postEvents) {
    boolean needPopulate = mScrollState == SCROLL_STATE_SETTLING;
    if (needPopulate) {
        // Done with scroll, no longer want to cache view drawing.
        setScrollingCacheEnabled(false);
        boolean wasScrolling = !mScroller.isFinished();
        if (wasScrolling) {
            mScroller.abortAnimation();
            int oldX = getScrollX();
            int oldY = getScrollY();
            int x = mScroller.getCurrX();
            int y = mScroller.getCurrY();
            if (oldX != x || oldY != y) {
                scrollTo(x, y);
                if (x != oldX) {
                    pageScrolled(x);
                }
            }
        }
    }
    mPopulatePending = false;
    for (int i = 0; i < mItems.size(); i++) {
        ItemInfo ii = mItems.get(i);
        if (ii.scrolling) {
            needPopulate = true;
            ii.scrolling = false;
        }
    }
    if (needPopulate) {
        if (postEvents) {
            ViewCompat.postOnAnimation(this, mEndScrollRunnable);
        } else {
            mEndScrollRunnable.run();
        }
    }
}
 
Example 6
Source File: AutoRefreshUtil.java    From SmoothRefreshLayout with MIT License 5 votes vote down vote up
@Override
public void run() {
    if (mRefreshLayout != null) {
        if (mNeedToTriggerRefresh && !mRefreshLayout.isNotYetInEdgeCannotMoveHeader()) {
            if (mRefreshLayout.autoRefresh(
                    mCachedActionAtOnce, mCachedAutoRefreshUseSmoothScroll)) {
                ScrollCompat.stopFling(mTargetView);
                mNeedToTriggerRefresh = false;
                mCachedActionAtOnce = false;
                mCachedAutoRefreshUseSmoothScroll = false;
                mRefreshLayout.removeCallbacks(this);
                return;
            }
        } else if (mNeedToTriggerLoadMore && !mRefreshLayout.isNotYetInEdgeCannotMoveFooter()) {
            if (mRefreshLayout.autoLoadMore(
                    mCachedActionAtOnce, mCachedAutoRefreshUseSmoothScroll)) {
                ScrollCompat.stopFling(mTargetView);
                mNeedToTriggerLoadMore = false;
                mCachedActionAtOnce = false;
                mCachedAutoRefreshUseSmoothScroll = false;
                mRefreshLayout.removeCallbacks(this);
                return;
            }
        }
        ViewCompat.postOnAnimation(mRefreshLayout, this);
    }
}
 
Example 7
Source File: AutoRefreshUtil.java    From SmoothRefreshLayout with MIT License 5 votes vote down vote up
public void autoLoadMore(boolean atOnce, boolean autoRefreshUseSmoothScroll) {
    if (mRefreshLayout != null) {
        if (mStatus != SmoothRefreshLayout.SR_STATUS_INIT) return;
        if (mRefreshLayout.isNotYetInEdgeCannotMoveFooter()) {
            if (mRefreshLayout.isVerticalOrientation()) {
                ScrollCompat.flingCompat(mTargetView, mMaximumFlingVelocity);
            } else {
                if (mTargetView instanceof ViewPager) {
                    final ViewPager pager = (ViewPager) mTargetView;
                    final PagerAdapter adapter = pager.getAdapter();
                    if (adapter == null) return;
                    if (adapter.getCount() <= 0) return;
                    pager.setCurrentItem(adapter.getCount() - 1, true);
                } else {
                    HorizontalScrollCompat.flingCompat(mTargetView, mMaximumFlingVelocity);
                }
            }
            mNeedToTriggerLoadMore = true;
            mCachedActionAtOnce = atOnce;
            mCachedAutoRefreshUseSmoothScroll = autoRefreshUseSmoothScroll;
        } else {
            mRefreshLayout.autoLoadMore(atOnce, autoRefreshUseSmoothScroll);
            mNeedToTriggerLoadMore = false;
            mCachedActionAtOnce = false;
            mCachedAutoRefreshUseSmoothScroll = false;
        }
        ViewCompat.postOnAnimation(mRefreshLayout, this);
    }
}
 
Example 8
Source File: RecyclerViewScrollerRunnable.java    From revolution-irc with GNU General Public License v3.0 5 votes vote down vote up
public void setScrollDir(int dir) {
    if (mScrollDir == dir)
        return;
    if (dir != 0 && mScrollDir == 0)
        ViewCompat.postOnAnimation(mRecyclerView, this);
    mScrollDir = dir;
    mPrevTime = AnimationUtils.currentAnimationTimeMillis();
}
 
Example 9
Source File: RecyclerBinder.java    From litho with Apache License 2.0 5 votes vote down vote up
private void requestRemeasure() {
  if (SectionsDebug.ENABLED) {
    Log.d(SectionsDebug.TAG, "(" + hashCode() + ") requestRemeasure");
  }

  if (mMountedView != null) {
    mMainThreadHandler.removeCallbacks(mRemeasureRunnable);
    mMountedView.removeCallbacks(mRemeasureRunnable);
    ViewCompat.postOnAnimation(mMountedView, mRemeasureRunnable);
  } else {
    // We are not mounted but we still need to post this. Just post on the main thread.
    mMainThreadHandler.removeCallbacks(mRemeasureRunnable);
    mMainThreadHandler.post(mRemeasureRunnable);
  }
}
 
Example 10
Source File: RecyclerBinder.java    From litho with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
  if (mMountedView == null || !mMountedView.hasPendingAdapterUpdates()) {
    if (mViewportManager.shouldUpdate()) {
      mViewportManager.onViewportChanged(State.DATA_CHANGES);
    }
    mPostUpdateViewportAttempts = 0;
    return;
  }

  // If the view gets detached, we might still have pending updates.
  // If the view's visibility is GONE, layout won't happen until it becomes visible. We have
  // to exit here, otherwise we keep posting this runnable to the next frame until it
  // becomes visible.
  if (!mMountedView.isAttachedToWindow() || mMountedView.getVisibility() == View.GONE) {
    mPostUpdateViewportAttempts = 0;
    return;
  }

  if (mPostUpdateViewportAttempts >= POST_UPDATE_VIEWPORT_AND_COMPUTE_RANGE_MAX_ATTEMPTS) {
    mPostUpdateViewportAttempts = 0;
    if (mViewportManager.shouldUpdate()) {
      mViewportManager.onViewportChanged(State.DATA_CHANGES);
    }

    return;
  }

  // If we have pending updates, wait until the sync operations are finished and try again
  // in the next frame.
  mPostUpdateViewportAttempts++;
  ViewCompat.postOnAnimation(mMountedView, mUpdateViewportRunnable);
}
 
Example 11
Source File: SmoothRefreshLayout.java    From SmoothRefreshLayout with MIT License 5 votes vote down vote up
void scrollTo(int to, int duration) {
    final int curPos = mIndicator.getCurrentPos();
    if (to > curPos) {
        stop();
        setInterpolator(mSpringInterpolator);
        mMode = Constants.SCROLLER_MODE_SPRING;
    } else if (to < curPos) {
        if (!mScrollChecker.isFlingBack()) {
            stop();
            mMode = Constants.SCROLLER_MODE_SPRING_BACK;
        }
        setInterpolator(mSpringBackInterpolator);
    } else {
        mMode = Constants.SCROLLER_MODE_NONE;
        return;
    }
    mLastStart = curPos;
    mLastTo = to;
    if (sDebug) {
        Log.d(
                TAG,
                String.format(
                        "ScrollChecker: scrollTo(): to: %d, duration: %d", to, duration));
    }
    int distance = (int) (mLastTo - mLastStart);
    mLastY = 0;
    mDuration = duration;
    mIsScrolling = true;
    mScroller.startScroll(0, 0, 0, distance, duration);
    removeCallbacks(this);
    if (duration <= 0) {
        run();
    } else {
        ViewCompat.postOnAnimation(SmoothRefreshLayout.this, this);
    }
}
 
Example 12
Source File: IncrementalMountHelper.java    From litho with Apache License 2.0 5 votes vote down vote up
void onAttach(LithoView lithoView) {
  if (!mComponentTree.isIncrementalMountEnabled()) {
    return;
  }

  // ViewPager does not give its child views any callbacks when it moves content onto the screen,
  // so we need to attach a listener to give us the information that we require.
  ViewParent viewParent = lithoView.getParent();
  while (viewParent != null) {
    if (viewParent instanceof ViewPager) {
      final ViewPager viewPager = (ViewPager) viewParent;
      final IncrementalMountHelper.ViewPagerListener viewPagerListener =
          new ViewPagerListener(mComponentTree, viewPager);

      // We want to add the listener immediately, since otherwise we might navigate to a
      // new tab in the ViewPager in this frame, and not mount the content. However, it is
      // possible that we are adding a listener here because its parent is being mounted due to
      // the ViewPager being scrolled (imagine a Recycler that is now on the screen and has to
      // mount a child view). In those cases adding the listener for the child will get a
      // ConcurrentModificationException, so we post it instead.
      try {
        viewPager.addOnPageChangeListener(viewPagerListener);
      } catch (ConcurrentModificationException e) {
        ViewCompat.postOnAnimation(
            viewPager,
            new Runnable() {
              @Override
              public void run() {
                viewPager.addOnPageChangeListener(viewPagerListener);
              }
            });
      }

      mViewPagerListeners.add(viewPagerListener);
    }

    viewParent = viewParent.getParent();
  }
}
 
Example 13
Source File: BottomSheetBehavior.java    From bottomsheetrecycler with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    if (viewDragHelper != null && viewDragHelper.continueSettling(true)) {
        ViewCompat.postOnAnimation(view, this);
    } else {
        if (state == STATE_SETTLING) {
            setStateInternal(targetState);
        }
    }
}
 
Example 14
Source File: ItemTouchHelper.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void run() {
    if (mSelected != null && scrollIfNecessary()) {
        if (mSelected != null) { //it might be lost during scrolling
            moveIfNecessary(mSelected);
        }
        mRecyclerView.removeCallbacks(mScrollRunnable);
        ViewCompat.postOnAnimation(mRecyclerView, this);
    }
}
 
Example 15
Source File: SliderPager.java    From Android-Image-Slider with Apache License 2.0 5 votes vote down vote up
private void completeScroll(boolean postEvents) {
    boolean needPopulate = mScrollState == SCROLL_STATE_SETTLING;
    if (needPopulate) {
        // Done with scroll, no longer want to cache view drawing.
        setScrollingCacheEnabled(false);
        boolean wasScrolling = !mScroller.isFinished();
        if (wasScrolling) {
            mScroller.abortAnimation();
            int oldX = getScrollX();
            int oldY = getScrollY();
            int x = mScroller.getCurrX();
            int y = mScroller.getCurrY();
            if (oldX != x || oldY != y) {
                scrollTo(x, y);
                if (x != oldX) {
                    pageScrolled(x);
                }
            }
        }
    }
    mPopulatePending = false;
    for (int i = 0; i < mItems.size(); i++) {
        ItemInfo ii = mItems.get(i);
        if (ii.scrolling) {
            needPopulate = true;
            ii.scrolling = false;
        }
    }
    if (needPopulate) {
        if (postEvents) {
            ViewCompat.postOnAnimation(this, mEndScrollRunnable);
        } else {
            mEndScrollRunnable.run();
        }
    }
}
 
Example 16
Source File: BottomSheetBehavior.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
  if (viewDragHelper != null && viewDragHelper.continueSettling(true)) {
    ViewCompat.postOnAnimation(view, this);
  } else {
    setStateInternal(targetState);
  }
  this.isPosted = false;
}
 
Example 17
Source File: CustomScrollView.java    From WidgetCase with Apache License 2.0 5 votes vote down vote up
private void postOnAnimation() {
    if (mEatRunOnAnimationRequest) {
        mReSchedulePostAnimationCallback = true;
    } else {
        removeCallbacks(this);
        ViewCompat.postOnAnimation(CustomScrollView.this, this);
    }
}
 
Example 18
Source File: SwipeDismissBehavior.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
  if (viewDragHelper != null && viewDragHelper.continueSettling(true)) {
    ViewCompat.postOnAnimation(view, this);
  } else {
    if (dismiss && listener != null) {
      listener.onDismiss(view);
    }
  }
}
 
Example 19
Source File: ViewAutoScroller.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
@Override
void runAtNextFrame(@NonNull Runnable r) {
    ViewCompat.postOnAnimation(mView, r);
}
 
Example 20
Source File: CustomScrollViewTest.java    From WidgetCase with Apache License 2.0 4 votes vote down vote up
private void postOnAnimation() {
    removeCallbacks(this);
    ViewCompat.postOnAnimation(CustomScrollViewTest.this, this);
}