Java Code Examples for androidx.recyclerview.widget.LinearLayoutManager#findFirstVisibleItemPosition()

The following examples show how to use androidx.recyclerview.widget.LinearLayoutManager#findFirstVisibleItemPosition() . 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: SelectionHandler.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void changeSelectionOfRecyclerView(CellRecyclerView recyclerView, AbstractViewHolder
        .SelectionState selectionState, @ColorInt int backgroundColor) {

    LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView
            .getLayoutManager();

    for (int i = linearLayoutManager.findFirstVisibleItemPosition(); i < linearLayoutManager
            .findLastVisibleItemPosition() + 1; i++) {

        AbstractViewHolder viewHolder = (AbstractViewHolder) recyclerView
                .findViewHolderForAdapterPosition(i);

        if (viewHolder != null) {
            if (!mTableView.isIgnoreSelectionColors()) {
                // Change background color
                viewHolder.setBackgroundColor(backgroundColor);
            }

            // Change selection status of the view holder
            viewHolder.setSelected(selectionState);
        }
    }
}
 
Example 2
Source File: SearchFragment.java    From hipda with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    if (dy > 0) {
        LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
        visibleItemCount = layoutManager.getChildCount();
        totalItemCount = layoutManager.getItemCount();
        int firstVisibleItem = layoutManager.findFirstVisibleItemPosition();

        if ((visibleItemCount + firstVisibleItem) >= totalItemCount - 5) {
            if (!mInloading) {
                mInloading = true;
                if (mPage < mMaxPage) {
                    mPage++;
                    mRecyclerView.setFooterState(XFooterView.STATE_LOADING);
                    SimpleListJob job = new SimpleListJob(getActivity(), mSessionId, mType, mPage, mSearchBean);
                    JobMgr.addJob(job);
                } else {
                    mRecyclerView.setFooterState(XFooterView.STATE_END);
                }
            }
        }
    }
}
 
Example 3
Source File: ConversationFragment.java    From deltachat-android with GNU General Public License v3.0 6 votes vote down vote up
private void manageMessageSeenState() {

        LinearLayoutManager layoutManager = (LinearLayoutManager)list.getLayoutManager();

        int firstPos = layoutManager.findFirstVisibleItemPosition();
        int lastPos = layoutManager.findLastVisibleItemPosition();
        if(firstPos == RecyclerView.NO_POSITION || lastPos == RecyclerView.NO_POSITION) {
            return;
        }

        int[] ids = new int[lastPos - firstPos + 1];
        int index = 0;
        for(int pos = firstPos; pos <= lastPos; pos++) {
            DcMsg message = ((ConversationAdapter) list.getAdapter()).getMsg(pos);
            if (message.getFromId() != DC_CONTACT_ID_SELF && !message.isSeen()) {
                ids[index] = message.getId();
                index++;
            }
        }
        dcContext.markseenMsgs(ids);
    }
 
Example 4
Source File: HorizontalRecyclerViewListener.java    From TableView with MIT License 6 votes vote down vote up
/**
 * This method calculates the current scroll position and its offset to help new attached
 * recyclerView on window at that position and offset
 *
 * @see #getScrollPosition()
 * @see #getScrollPositionOffset()
 */
private void renewScrollPosition(@NonNull RecyclerView recyclerView) {
    LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
    mScrollPosition = layoutManager.findFirstCompletelyVisibleItemPosition();

    // That means there is no completely visible Position.
    if (mScrollPosition == -1) {
        mScrollPosition = layoutManager.findFirstVisibleItemPosition();

        // That means there is just a visible item on the screen
        if (mScrollPosition == layoutManager.findLastVisibleItemPosition()) {
            // in this case we use the position which is the last & first visible item.
        } else {
            // That means there are 2 visible item on the screen. However, second one is not
            // completely visible.
            mScrollPosition = mScrollPosition + 1;
        }
    }

    mScrollPositionOffset = layoutManager.findViewByPosition(mScrollPosition).getLeft();
}
 
Example 5
Source File: SelectionHandler.java    From TableView with MIT License 6 votes vote down vote up
public void changeSelectionOfRecyclerView(CellRecyclerView recyclerView, @NonNull AbstractViewHolder
        .SelectionState selectionState, @ColorInt int backgroundColor) {

    LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView
            .getLayoutManager();

    for (int i = linearLayoutManager.findFirstVisibleItemPosition(); i < linearLayoutManager
            .findLastVisibleItemPosition() + 1; i++) {

        AbstractViewHolder viewHolder = (AbstractViewHolder) recyclerView
                .findViewHolderForAdapterPosition(i);

        if (viewHolder != null) {
            if (!mTableView.isIgnoreSelectionColors()) {
                // Change background color
                viewHolder.setBackgroundColor(backgroundColor);
            }

            // Change selection status of the view holder
            viewHolder.setSelected(selectionState);
        }
    }
}
 
Example 6
Source File: CommentListingFragment.java    From RedReader with GNU General Public License v3.0 6 votes vote down vote up
public void handleCommentVisibilityToggle(final RedditCommentView view) {

		final RedditChangeDataManager changeDataManager = RedditChangeDataManager.getInstance(mUser);
		final RedditCommentListItem item = view.getComment();

		if(item.isComment()) {
			final RedditRenderableComment comment = item.asComment();

			changeDataManager.markHidden(
					RRTime.utcCurrentTimeMillis(),
					comment,
					!comment.isCollapsed(changeDataManager));

			mCommentListingManager.updateHiddenStatus();

			final LinearLayoutManager layoutManager = (LinearLayoutManager)mRecyclerView.getLayoutManager();
			final int position = layoutManager.getPosition(view);

			if(position == layoutManager.findFirstVisibleItemPosition()) {
				layoutManager.scrollToPositionWithOffset(position, 0);
			}
		}
	}
 
Example 7
Source File: ThreadListFragment.java    From hipda with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    if (dy > 0) {
        LinearLayoutManager mLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
        visibleItemCount = mLayoutManager.getChildCount();
        totalItemCount = mLayoutManager.getItemCount();
        mFirstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();

        if ((visibleItemCount + mFirstVisibleItem) >= totalItemCount - 5) {
            if (!mInloading) {
                mPage++;
                mInloading = true;
                mRecyclerView.setFooterState(XFooterView.STATE_LOADING);
                ThreadListJob job = new ThreadListJob(getActivity(), mSessionId, mForumId, mPage);
                JobMgr.addJob(job);
            }
        }
    }
}
 
Example 8
Source File: UserinfoFragment.java    From hipda with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    if (dy > 0) {
        LinearLayoutManager mLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
        visibleItemCount = mLayoutManager.getChildCount();
        totalItemCount = mLayoutManager.getItemCount();
        int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();

        if ((visibleItemCount + firstVisibleItem) >= totalItemCount - 5) {
            if (!mInloading) {
                mInloading = true;
                if (mPage < mMaxPage) {
                    mPage++;
                    mRecyclerView.setFooterState(XFooterView.STATE_LOADING);
                    SearchBean bean = new SearchBean();
                    bean.setUid(mUid);
                    bean.setSearchId(mSearchId);
                    SimpleListJob job = new SimpleListJob(UserinfoFragment.this.getActivity(), mSessionId,
                            SimpleListJob.TYPE_SEARCH_USER_THREADS,
                            mPage,
                            bean);
                    JobMgr.addJob(job);
                } else {
                    mRecyclerView.setFooterState(XFooterView.STATE_END);
                }
            }
        }
    }
}
 
Example 9
Source File: ScrollPositionListener.java    From Hentoid with Apache License 2.0 5 votes vote down vote up
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    LinearLayoutManager llm = (LinearLayoutManager) recyclerView.getLayoutManager();
    if (llm != null) {
        int firstVisibleItemPosition = llm.findFirstVisibleItemPosition();
        int lastCompletelyVisibleItemPosition = llm.findLastCompletelyVisibleItemPosition();
        onPositionChangeListener.accept(Math.max(firstVisibleItemPosition, lastCompletelyVisibleItemPosition));
    }
}
 
Example 10
Source File: ZoomableRecyclerView.java    From Hentoid with Apache License 2.0 5 votes vote down vote up
@Override
public void onScrolled(int dx, int dy) {
    super.onScrolled(dx, dy);
    LinearLayoutManager layoutManager = (LinearLayoutManager) getLayoutManager();
    if (layoutManager != null) {
        lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
        firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
    }
}
 
Example 11
Source File: VideoTrimmerView.java    From Android-Video-Trimmer with Apache License 2.0 5 votes vote down vote up
/**
 * 水平滑动了多少px
 */
private int calcScrollXDistance() {
  LinearLayoutManager layoutManager = (LinearLayoutManager) mVideoThumbRecyclerView.getLayoutManager();
  int position = layoutManager.findFirstVisibleItemPosition();
  View firstVisibleChildView = layoutManager.findViewByPosition(position);
  int itemWidth = firstVisibleChildView.getWidth();
  return (position) * itemWidth - firstVisibleChildView.getLeft();
}
 
Example 12
Source File: AsyncAdapter.java    From financisto with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void getItemRangeInto(int[] outRange) {
    if (outRange == null) {
        return;
    }
    if(recyclerView.getLayoutManager() instanceof LinearLayoutManager){
        LinearLayoutManager llm = (LinearLayoutManager) recyclerView.getLayoutManager();
        outRange[0] = llm.findFirstVisibleItemPosition();
        outRange[1] = llm.findLastVisibleItemPosition();
    }
    if (outRange[0] == -1 && outRange[1] == -1) {
        outRange[0] = 0;
        outRange[1] = 0;
    }
}
 
Example 13
Source File: HorizontalScrollCompat.java    From SmoothRefreshLayout with MIT License 5 votes vote down vote up
public static boolean canAutoRefresh(View view) {
    if (ViewCatcherUtil.isRecyclerView(view)) {
        RecyclerView recyclerView = (RecyclerView) view;
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager == null) {
            return false;
        }
        int firstVisiblePosition = -1;
        if (manager instanceof LinearLayoutManager) {
            LinearLayoutManager linearManager = ((LinearLayoutManager) manager);
            if (linearManager.getOrientation() != RecyclerView.HORIZONTAL) {
                return false;
            }
            firstVisiblePosition = linearManager.findFirstVisibleItemPosition();
        } else if (manager instanceof StaggeredGridLayoutManager) {
            StaggeredGridLayoutManager gridLayoutManager = (StaggeredGridLayoutManager) manager;
            if (gridLayoutManager.getOrientation() != RecyclerView.HORIZONTAL) {
                return false;
            }
            int[] firstPositions = new int[gridLayoutManager.getSpanCount()];
            gridLayoutManager.findFirstVisibleItemPositions(firstPositions);
            for (int value : firstPositions) {
                if (value == 0) {
                    firstVisiblePosition = 0;
                    break;
                }
            }
        }
        RecyclerView.Adapter adapter = recyclerView.getAdapter();
        return adapter != null && firstVisiblePosition == 0;
    }
    return false;
}
 
Example 14
Source File: EndlessScrollListener.java    From RendererRecyclerViewAdapter with Apache License 2.0 5 votes vote down vote up
@Override
public void onScrolled(final RecyclerView mRecyclerView, final int dx, final int dy) {
	super.onScrolled(mRecyclerView, dx, dy);
	final LinearLayoutManager mLayoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();

	visibleItemCount = mRecyclerView.getChildCount();
	totalItemCount = mLayoutManager.getItemCount();
	firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
	onScroll(firstVisibleItem, visibleItemCount, totalItemCount);
}
 
Example 15
Source File: GamesTutorial.java    From PretendYoureXyzzyAndroid with GNU General Public License v3.0 5 votes vote down vote up
public final boolean buildSequence(@NonNull View createGame, @NonNull RecyclerView list) {
    LinearLayoutManager llm = (LinearLayoutManager) list.getLayoutManager();
    if (llm == null) return false;

    int pos = llm.findFirstVisibleItemPosition();
    if (pos == -1) return false;

    GamesAdapter.ViewHolder holder = (GamesAdapter.ViewHolder) list.findViewHolderForLayoutPosition(pos);
    if (holder != null) {
        add(forView(holder.status, R.string.tutorial_gameStatus)
                .focusShape(FocusShape.CIRCLE)
                .enableAutoTextPosition()
                .fitSystemWindows(true));
        add(forView(holder.locked, R.string.tutorial_gameLocked)
                .focusShape(FocusShape.CIRCLE)
                .enableAutoTextPosition()
                .fitSystemWindows(true));
        add(forView(holder.spectate, R.string.tutorial_spectateGame)
                .focusShape(FocusShape.ROUNDED_RECTANGLE)
                .roundRectRadius(8)
                .enableAutoTextPosition()
                .fitSystemWindows(true));
        add(forView(holder.join, R.string.tutorial_joinGame)
                .focusShape(FocusShape.ROUNDED_RECTANGLE)
                .roundRectRadius(8)
                .enableAutoTextPosition()
                .fitSystemWindows(true));
        add(forView(createGame, R.string.tutorial_createGame)
                .focusShape(FocusShape.CIRCLE)
                .enableAutoTextPosition()
                .fitSystemWindows(true));
        return true;
    }

    return false;
}
 
Example 16
Source File: StickyItemDecoration.java    From AndroidAnimationExercise with Apache License 2.0 4 votes vote down vote up
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    super.onDrawOver(c, parent, state);

    if (parent.getAdapter().getItemCount() <= 0) return;

    mLayoutManager = (LinearLayoutManager) parent.getLayoutManager();
    mCurrentUIFindStickView = false;

    for (int m = 0, size = parent.getChildCount(); m < size; m++) {
        View view = parent.getChildAt(m);

        /**
         * 如果是吸附的view
         */
        if (mStickyView.isStickyView(view)) {
            mCurrentUIFindStickView = true;
            getStickyViewHolder(parent);
            cacheStickyViewPosition(m);

            if (view.getTop() <= 0) {
                bindDataForStickyView(mLayoutManager.findFirstVisibleItemPosition(), parent.getMeasuredWidth());
            } else {
                if (mStickyPositionList.size() > 0) {
                    if (mStickyPositionList.size() == 1) {
                        bindDataForStickyView(mStickyPositionList.get(0), parent.getMeasuredWidth());
                    } else {
                        int currentPosition = getStickyViewPositionOfRecyclerView(m);
                        int indexOfCurrentPosition = mStickyPositionList.lastIndexOf(currentPosition);
                        if (indexOfCurrentPosition >= 1) bindDataForStickyView(mStickyPositionList.get(indexOfCurrentPosition - 1), parent.getMeasuredWidth());
                    }
                }
            }

            if (view.getTop() > 0 && view.getTop() <= mStickyItemViewHeight) {
                mStickyItemViewMarginTop = mStickyItemViewHeight - view.getTop();
            } else {
                mStickyItemViewMarginTop = 0;

                View nextStickyView = getNextStickyView(parent);
                if (nextStickyView != null && nextStickyView.getTop() <= mStickyItemViewHeight) {
                    mStickyItemViewMarginTop = mStickyItemViewHeight - nextStickyView.getTop();
                }

            }

            drawStickyItemView(c);
            break;
        }
    }

    if (!mCurrentUIFindStickView) {
        mStickyItemViewMarginTop = 0;
        if (mLayoutManager.findFirstVisibleItemPosition() + parent.getChildCount() == parent.getAdapter().getItemCount() && mStickyPositionList.size() > 0) {
            bindDataForStickyView(mStickyPositionList.get(mStickyPositionList.size() - 1), parent.getMeasuredWidth());
        }
        drawStickyItemView(c);
    }
}
 
Example 17
Source File: BackToTopUtils.java    From Mysplash with GNU Lesser General Public License v3.0 4 votes vote down vote up
private static int getFirstVisibleItemPosition(LinearLayoutManager manager) {
    return manager.findFirstVisibleItemPosition();
}
 
Example 18
Source File: RecyclerViewStickyHeadItemDecoration.java    From RecyclerViewDecoration with Apache License 2.0 4 votes vote down vote up
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    super.onDrawOver(c, parent, state);

    if (parent ==null || parent.getAdapter() == null || parent.getAdapter().getItemCount() == 0
            || parent.getLayoutManager() == null) {
        return;
    }

    if (parent.getLayoutManager() instanceof LinearLayoutManager) {
        LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();

        boolean findStickView = false;

        int itemCount = parent.getChildCount();
        for (int i = 0; i < itemCount; i++) {
            View view = parent.getChildAt(i);

            if (isGroupViewType(parent.getChildAdapterPosition(view))) {
                findStickView = true;
                if (mCurrentStickyViewHolder == null) {
                    mCurrentStickyViewHolder = parent.getAdapter().onCreateViewHolder(parent, mGroupViewType);
                    mStickyView = mCurrentStickyViewHolder.itemView;
                }
                if (view.getTop() <= 0) {
                    bindDataForStickyView(layoutManager.findFirstVisibleItemPosition());
                } else {
                    if (mStickyPositionList.size() > 0) {
                        if (mStickyPositionList.size() == 1) {
                            bindDataForStickyView(mStickyPositionList.get(0));
                        } else {
                            int currentPosition = layoutManager.findFirstVisibleItemPosition() + i;
                            int indexOfCurrentPosition = mStickyPositionList.lastIndexOf(currentPosition);
                            bindDataForStickyView(mStickyPositionList.get(indexOfCurrentPosition - 1));
                        }
                    }
                }

                if (view.getTop() > 0 && view.getTop() <= mStickyViewHeight) {
                    mStickyViewMarginTop = mStickyViewHeight - view.getTop();
                } else {
                    mStickyViewMarginTop = 0;

                    View nextStickyView = getNextStickyView();
                    if (nextStickyView != null && nextStickyView.getTop() <= mStickyViewHeight) {
                        mStickyViewMarginTop = mStickyViewHeight - nextStickyView.getTop();
                    }

                }

                drawStickyView(c);
                break;
            }
        }

        if (!findStickView) {
            mStickyViewMarginTop = 0;
            if (layoutManager.findFirstVisibleItemPosition() + parent.getChildCount() == parent.getAdapter().getItemCount()
                    && mStickyPositionList.size() > 0) {
                bindDataForStickyView(mStickyPositionList.get(mStickyPositionList.size() - 1));
            }
            drawStickyView(c);
        }
    } else {
        try {
            throw new IllegalAccessException("Only support RecyclerView LinearLayoutManager.VERTICAL");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
 
Example 19
Source File: ScrollPositionListener.java    From Hentoid with Apache License 2.0 4 votes vote down vote up
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
    super.onScrollStateChanged(recyclerView, newState);
    if (!Preferences.isViewerSwipeToTurn() || !isScrollEnabled) {
        recyclerView.stopScroll();
        return;
    }

    LinearLayoutManager llm = (LinearLayoutManager) recyclerView.getLayoutManager();
    if (llm != null) {
        if (RecyclerView.SCROLL_STATE_DRAGGING == newState) {
            dragStartPositionX = recyclerView.computeHorizontalScrollOffset();
            dragStartPositionY = recyclerView.computeVerticalScrollOffset();
            isSettlingX = false;
            isSettlingY = false;
        } else if (RecyclerView.SCROLL_STATE_SETTLING == newState) {
            // If the settling position is different from the original position, ignore that scroll
            // (e.g. snapping back to the original position after a small scroll)
            if (recyclerView.computeHorizontalScrollOffset() != dragStartPositionX)
                isSettlingX = true;
            if (recyclerView.computeVerticalScrollOffset() != dragStartPositionY)
                isSettlingY = true;
        } else if (RecyclerView.SCROLL_STATE_IDLE == newState) {
            // Don't do anything if we're not on a boundary
            if (!(llm.findLastVisibleItemPosition() == llm.getItemCount() - 1 || 0 == llm.findFirstVisibleItemPosition()))
                return;

            if (recyclerView.computeHorizontalScrollOffset() == dragStartPositionX && !isSettlingX && llm.canScrollHorizontally()) {
                if (0 == dragStartPositionX && !llm.getReverseLayout())
                    onStartOutOfBoundScroll.run();
                else if (0 == dragStartPositionX) onEndOutOfBoundScroll.run();
                else if (llm.getReverseLayout()) onStartOutOfBoundScroll.run();
                else onEndOutOfBoundScroll.run();
            }
            if (recyclerView.computeVerticalScrollOffset() == dragStartPositionY && !isSettlingY && llm.canScrollVertically()) {
                if (0 == dragStartPositionY && !llm.getReverseLayout())
                    onStartOutOfBoundScroll.run();
                else if (0 == dragStartPositionY) onEndOutOfBoundScroll.run();
                else if (llm.getReverseLayout()) onStartOutOfBoundScroll.run();
                else onEndOutOfBoundScroll.run();
            }
        }
    }
}
 
Example 20
Source File: AlScrollListener.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);

    RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();

    visibleItemCount = recyclerView.getChildCount();
    if (manager instanceof GridLayoutManager) {
        GridLayoutManager gridLayoutManager = (GridLayoutManager) manager;
        firstVisibleItem = gridLayoutManager.findFirstVisibleItemPosition();
        totalItemCount = gridLayoutManager.getItemCount();
    } else if (manager instanceof LinearLayoutManager) {
        LinearLayoutManager linearLayoutManager = (LinearLayoutManager) manager;
        firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
        totalItemCount = linearLayoutManager.getItemCount();
    }


    if (infiniteScrollingEnabled) {
        if (loading) {
            if (totalItemCount > previousTotal) {
                loading = false;
                previousTotal = totalItemCount;
            }
        }

        if ((totalItemCount - visibleItemCount) == 0) {
            return;
        }


        if (!loading && (totalItemCount - visibleItemCount <= firstVisibleItem + visibleThreshold)) {
            // End has been reached
            // do something
            onLoadMore();
            loading = true;
        }
    }

    if (firstVisibleItem == 0) {
        if (!controlsVisible) {
            onScrollUp();
            controlsVisible = true;
        }

        return;
    }

    if (scrolledDistance > HIDE_THRESHOLD && controlsVisible) {
        onScrollDown();
        controlsVisible = false;
        scrolledDistance = 0;
    } else if (scrolledDistance < -HIDE_THRESHOLD && !controlsVisible) {
        onScrollUp();
        controlsVisible = true;
        scrolledDistance = 0;
    }

    if ((controlsVisible && dy > 0) || (!controlsVisible && dy < 0)) {
        scrolledDistance += dy;
    }
}