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

The following examples show how to use androidx.recyclerview.widget.LinearLayoutManager#scrollToPositionWithOffset() . 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: FlatPlayerFragment.java    From Music-Player with GNU General Public License v3.0 6 votes vote down vote up
private void setUpRecyclerView() {
    recyclerViewDragDropManager = new RecyclerViewDragDropManager();
    final GeneralItemAnimator animator = new RefactoredDefaultItemAnimator();

    playingQueueAdapter = new PlayingQueueAdapter(
            ((AppCompatActivity) getActivity()),
            MusicPlayerRemote.getPlayingQueue(),
            MusicPlayerRemote.getPosition(),
            R.layout.item_list,
            false,
            null);
    wrappedAdapter = recyclerViewDragDropManager.createWrappedAdapter(playingQueueAdapter);

    layoutManager = new LinearLayoutManager(getActivity());

    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(wrappedAdapter);
    recyclerView.setItemAnimator(animator);

    recyclerViewDragDropManager.attachRecyclerView(recyclerView);

    layoutManager.scrollToPositionWithOffset(MusicPlayerRemote.getPosition() + 1, 0);
}
 
Example 2
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 3
Source File: PostListingFragment.java    From RedReader with GNU General Public License v3.0 6 votes vote down vote up
public void onPostsAdded() {

		if(mPreviousFirstVisibleItemPosition == null) {
			return;
		}

		final LinearLayoutManager layoutManager = (LinearLayoutManager)mRecyclerView.getLayoutManager();

		if(layoutManager.getItemCount() > mPreviousFirstVisibleItemPosition) {
			layoutManager.scrollToPositionWithOffset(mPreviousFirstVisibleItemPosition, 0);
			mPreviousFirstVisibleItemPosition = null;

		} else {
			layoutManager.scrollToPosition(layoutManager.getItemCount() - 1);
		}
	}
 
Example 4
Source File: FromRecyclerViewListener.java    From GestureViews with Apache License 2.0 6 votes vote down vote up
@Override
void scrollToPosition(RecyclerView list, int pos) {
    if (list.getLayoutManager() instanceof LinearLayoutManager) {
        // Centering item in its parent
        final LinearLayoutManager manager = (LinearLayoutManager) list.getLayoutManager();
        final boolean isHorizontal = manager.getOrientation() == LinearLayoutManager.HORIZONTAL;

        int offset = isHorizontal
                ? (list.getWidth() - list.getPaddingLeft() - list.getPaddingRight()) / 2
                : (list.getHeight() - list.getPaddingTop() - list.getPaddingBottom()) / 2;

        final RecyclerView.ViewHolder holder = list.findViewHolderForAdapterPosition(pos);
        if (holder != null) {
            final View view = holder.itemView;
            offset -= isHorizontal ? view.getWidth() / 2 : view.getHeight() / 2;
        }

        manager.scrollToPositionWithOffset(pos, offset);
    } else {
        list.scrollToPosition(pos);
    }
}
 
Example 5
Source File: CardPlayerFragment.java    From Phonograph with GNU General Public License v3.0 6 votes vote down vote up
private void setUpRecyclerView() {
    recyclerViewDragDropManager = new RecyclerViewDragDropManager();
    final GeneralItemAnimator animator = new RefactoredDefaultItemAnimator();

    playingQueueAdapter = new PlayingQueueAdapter(
            ((AppCompatActivity) getActivity()),
            MusicPlayerRemote.getPlayingQueue(),
            MusicPlayerRemote.getPosition(),
            R.layout.item_list,
            false,
            null);
    wrappedAdapter = recyclerViewDragDropManager.createWrappedAdapter(playingQueueAdapter);

    layoutManager = new LinearLayoutManager(getActivity());

    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(wrappedAdapter);
    recyclerView.setItemAnimator(animator);

    recyclerViewDragDropManager.attachRecyclerView(recyclerView);

    layoutManager.scrollToPositionWithOffset(MusicPlayerRemote.getPosition() + 1, 0);
}
 
Example 6
Source File: FlatPlayerFragment.java    From Phonograph with GNU General Public License v3.0 6 votes vote down vote up
private void setUpRecyclerView() {
    recyclerViewDragDropManager = new RecyclerViewDragDropManager();
    final GeneralItemAnimator animator = new RefactoredDefaultItemAnimator();

    playingQueueAdapter = new PlayingQueueAdapter(
            ((AppCompatActivity) getActivity()),
            MusicPlayerRemote.getPlayingQueue(),
            MusicPlayerRemote.getPosition(),
            R.layout.item_list,
            false,
            null);
    wrappedAdapter = recyclerViewDragDropManager.createWrappedAdapter(playingQueueAdapter);

    layoutManager = new LinearLayoutManager(getActivity());

    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(wrappedAdapter);
    recyclerView.setItemAnimator(animator);

    recyclerViewDragDropManager.attachRecyclerView(recyclerView);

    layoutManager.scrollToPositionWithOffset(MusicPlayerRemote.getPosition() + 1, 0);
}
 
Example 7
Source File: CenteringRecyclerView.java    From centering-recycler-view with Apache License 2.0 6 votes vote down vote up
/**
 * Scrolls a view at the given position to top (vertical layout) or left (horizontal layout).
 *
 * @param position The adapter position.
 */
public void head(int position) {
    if (mIgnoreIfCompletelyVisible && isCompletelyVisible(position)) {
        return;
    }

    if (mIgnoreIfVisible && isVisible(position)) {
        return;
    }

    LayoutManager lm = getLayoutManager();
    if (lm instanceof LinearLayoutManager) {
        LinearLayoutManager llm = (LinearLayoutManager) lm;
        llm.scrollToPositionWithOffset(position, 0);
    } else if (lm instanceof StaggeredGridLayoutManager) {
        final StaggeredGridLayoutManager sglm = (StaggeredGridLayoutManager) lm;
        sglm.scrollToPositionWithOffset(position, 0);
    } else {
        throw new UnsupportedOperationException("unsupported layout manager");
    }
}
 
Example 8
Source File: NowPlayingFragment.java    From Audinaut with GNU General Public License v3.0 6 votes vote down vote up
private void scrollToCurrent() {
    if (getDownloadService() == null || songListAdapter == null) {
        scrollWhenLoaded = true;
        return;
    }

    // Try to get position of current playing/downloading
    int position = songListAdapter.getItemPosition(currentPlaying);
    if (position == -1) {
        DownloadFile currentDownloading = getDownloadService().getCurrentDownloading();
        position = songListAdapter.getItemPosition(currentDownloading);
    }

    // If found, scroll to it
    if (position != -1) {
        // RecyclerView.scrollToPosition just puts it on the screen (ie: bottom if scrolled below it)
        LinearLayoutManager layoutManager = (LinearLayoutManager) playlistView.getLayoutManager();
        layoutManager.scrollToPositionWithOffset(position, 0);
    }
}
 
Example 9
Source File: MainActivity.java    From GetApk with MIT License 6 votes vote down vote up
@Override
public void onLetterChange(String letter) {
    if (mRecyclerView.getAdapter() == null){
        return;
    }
    ItemArray itemArray = ((AppAdapter) mRecyclerView.getAdapter()).getItems();
    int size = itemArray.size();
    for (int i = 0; i < size; i++) {
        ItemData data = itemArray.get(i);
        if (data.getDataType() == AppAdapter.TYPE_STICKY) {
            App app = data.getData();
            if (app.namePinyin.startsWith(letter)) {
                LinearLayoutManager mLayoutManager =
                        (LinearLayoutManager) mRecyclerView.getLayoutManager();
                mLayoutManager.scrollToPositionWithOffset(i, 0);
                return;
            }
        }
    }
}
 
Example 10
Source File: CardPlayerFragment.java    From Music-Player with GNU General Public License v3.0 6 votes vote down vote up
private void setUpRecyclerView() {
    recyclerViewDragDropManager = new RecyclerViewDragDropManager();
    final GeneralItemAnimator animator = new RefactoredDefaultItemAnimator();

    playingQueueAdapter = new PlayingQueueAdapter(
            ((AppCompatActivity) getActivity()),
            MusicPlayerRemote.getPlayingQueue(),
            MusicPlayerRemote.getPosition(),
            R.layout.item_list,
            false,
            null);
    wrappedAdapter = recyclerViewDragDropManager.createWrappedAdapter(playingQueueAdapter);

    layoutManager = new LinearLayoutManager(getActivity());

    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(wrappedAdapter);
    recyclerView.setItemAnimator(animator);

    recyclerViewDragDropManager.attachRecyclerView(recyclerView);

    layoutManager.scrollToPositionWithOffset(MusicPlayerRemote.getPosition() + 1, 0);
}
 
Example 11
Source File: OptimumRecyclerView.java    From RvHelper with Apache License 2.0 5 votes vote down vote up
public void move(int n) {
    if (n < 0 || n >= getAdapter().getItemCount()) {
        PtrCLog.e(TAG, "move: index error");
        return;
    }
    mRecyclerView.stopScroll();
    LinearLayoutManager linearLayoutManager = (LinearLayoutManager) getRecyclerView().getLayoutManager();
    linearLayoutManager.scrollToPositionWithOffset(n, 0);
}
 
Example 12
Source File: ScrollingUtilities.java    From MaterialScrollBar with Apache License 2.0 5 votes vote down vote up
/**
 * Scrolls to the specified fraction of the RV
 *
 * @param touchFraction the fraction of the RV to scroll through
 * @return the distance traveled by the RV in the transformation applied by this method.
 * + is downward, - upward.
 */
int scrollToPositionAtProgress(float touchFraction) {
    int priorPosition = materialScrollBar.recyclerView.computeVerticalScrollOffset();
    int exactItemPos;
    if(customScroller == null) {
        int spanCount = 1;
        if(materialScrollBar.recyclerView.getLayoutManager() instanceof GridLayoutManager) {
            spanCount = ((GridLayoutManager) materialScrollBar.recyclerView.getLayoutManager()).getSpanCount();
        }

        // Stop the scroller if it is scrolling
        materialScrollBar.recyclerView.stopScroll();

        getCurScrollState();

        //The exact position of our desired item
        exactItemPos = (int) (getAvailableScrollHeight() * touchFraction);

        //Scroll to the desired item. The offset used here is kind of hard to explain.
        //If the position we wish to scroll to is, say, position 10.5, we scroll to position 10,
        //and then offset by 0.5 * rowHeight. This is how we achieve smooth scrolling.
        LinearLayoutManager layoutManager = ((LinearLayoutManager) materialScrollBar.recyclerView.getLayoutManager());
        try {
            layoutManager.scrollToPositionWithOffset(spanCount * exactItemPos / scrollPosState.rowHeight,
                    -(exactItemPos % scrollPosState.rowHeight));
        } catch (ArithmeticException e) { /* Avoids issues where children of RV have not yet been laid out */ }
    } else {
        if(layoutManager == null) {
            layoutManager = ((LinearLayoutManager) materialScrollBar.recyclerView.getLayoutManager());
        }
        exactItemPos = customScroller.getItemIndexForScroll(touchFraction);
        int offset = (int) (customScroller.getDepthForItem(exactItemPos) - touchFraction * getAvailableScrollHeight());
        layoutManager.scrollToPositionWithOffset(exactItemPos, offset);
        return 0;
    }
    return exactItemPos - priorPosition;
}
 
Example 13
Source File: ArticleListsFragment.java    From android-app with GNU General Public License v3.0 5 votes vote down vote up
public void scroll(boolean up) {
    ArticleListFragment currentFragment = getCurrentFragment();

    if (currentFragment != null && currentFragment.recyclerViewLayoutManager != null) {
        LinearLayoutManager listLayout = currentFragment.recyclerViewLayoutManager;

        int numberOfVisibleItems =
                listLayout.findLastCompletelyVisibleItemPosition() -
                        listLayout.findFirstCompletelyVisibleItemPosition() + 1;

        int oldPositionOnTop = listLayout.findFirstCompletelyVisibleItemPosition();

        // scroll so that as many new articles are visible than possible with one overlap
        int newPositionOnTop;
        if (up) {
            newPositionOnTop = oldPositionOnTop - numberOfVisibleItems + 1;
        } else {
            newPositionOnTop = oldPositionOnTop + numberOfVisibleItems - 1;
        }

        if (newPositionOnTop >= listLayout.getItemCount()) {
            newPositionOnTop = listLayout.getItemCount() - numberOfVisibleItems - 1;
        } else if (newPositionOnTop < 0) {
            newPositionOnTop = 0;
        }

        Log.v(TAG, " scrolling to position: " + newPositionOnTop);

        listLayout.scrollToPositionWithOffset(newPositionOnTop, 0);
    }
}
 
Example 14
Source File: RecyclerViewHelper.java    From AndroidFastScroll with Apache License 2.0 5 votes vote down vote up
private void scrollToPositionWithOffset(int position, int offset) {
    LinearLayoutManager linearLayoutManager = getVerticalLinearLayoutManager();
    if (linearLayoutManager == null) {
        return;
    }
    if (linearLayoutManager instanceof GridLayoutManager) {
        GridLayoutManager gridLayoutManager = (GridLayoutManager) linearLayoutManager;
        position *= gridLayoutManager.getSpanCount();
    }
    // LinearLayoutManager actually takes offset from paddingTop instead of top of RecyclerView.
    offset -= mView.getPaddingTop();
    linearLayoutManager.scrollToPositionWithOffset(position, offset);
}
 
Example 15
Source File: UpFetchUseActivity.java    From BaseRecyclerViewAdapterHelper with MIT License 4 votes vote down vote up
/**
 * 滚动到底部(不带动画)
 */
private void scrollToBottom() {
    LinearLayoutManager ll = (LinearLayoutManager) mRecyclerView.getLayoutManager();
    ll.scrollToPositionWithOffset(getBottomDataPosition(), 0);
}
 
Example 16
Source File: FastScroller.java    From Audinaut with GNU General Public License v3.0 4 votes vote down vote up
private void setRecyclerViewPosition(float y) {
    if (recyclerView != null) {
        if (recyclerView.getChildCount() == 0) {
            return;
        }

        int itemCount = recyclerView.getAdapter().getItemCount();
        float proportion = getValueInRange(1f, y / (float) height);

        float targetPosFloat = getValueInRange(itemCount - 1, proportion * (float) itemCount);
        int targetPos = (int) targetPosFloat;

        // Immediately make sure that the target is visible
        LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
        // layoutManager.scrollToPositionWithOffset(targetPos, 0);
        View firstVisibleView = recyclerView.getChildAt(0);

        // Calculate how far through this position we are
        int columns = Math.round(recyclerView.getWidth() / firstVisibleView.getWidth());
        int firstVisiblePosition = recyclerView.getChildAdapterPosition(firstVisibleView);
        int remainder = (targetPos - firstVisiblePosition) % columns;
        float offsetPercentage = (targetPosFloat - targetPos + remainder) / columns;
        if (offsetPercentage < 0) {
            offsetPercentage = 1 + offsetPercentage;
        }
        int firstVisibleHeight = firstVisibleView.getHeight();
        if (columns > 1) {
            firstVisibleHeight += (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, GridSpacingDecoration.SPACING, firstVisibleView.getResources().getDisplayMetrics());
        }
        int offset = (int) (offsetPercentage * firstVisibleHeight);

        layoutManager.scrollToPositionWithOffset(targetPos, -offset);
        onUpdateScroll(1, 1);

        try {
            String bubbleText = null;
            RecyclerView.Adapter adapter = recyclerView.getAdapter();
            if (adapter instanceof BubbleTextGetter) {
                bubbleText = ((BubbleTextGetter) adapter).getTextToShowInBubble(targetPos);
            }

            if (bubbleText == null) {
                visibleBubble = false;
                bubble.setVisibility(View.INVISIBLE);
            } else {
                bubble.setText(bubbleText);
                bubble.setVisibility(View.VISIBLE);
                visibleBubble = true;
            }
        } catch (Exception e) {
            Log.e(TAG, "Error getting text for bubble", e);
        }
    }
}
 
Example 17
Source File: FastScrollRecyclerView.java    From MusicPlayer with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Maps the touch (from 0..1) to the adapter position that should be visible.
 */
public String scrollToPositionAtProgress(float touchFraction) {
    int itemCount = getAdapter().getItemCount();
    if (itemCount == 0) {
        return "";
    }
    int spanCount = 1;
    int rowCount = itemCount;
    if (getLayoutManager() instanceof GridLayoutManager) {
        spanCount = ((GridLayoutManager) getLayoutManager()).getSpanCount();
        rowCount = (int) Math.ceil((double) rowCount / spanCount);
    }

    // Stop the scroller if it is scrolling
    stopScroll();

    getCurScrollState(mScrollPosState);

    float itemPos = itemCount * touchFraction;

    int availableScrollHeight = getAvailableScrollHeight(rowCount, mScrollPosState.rowHeight, 0);

    //The exact position of our desired item
    int exactItemPos = (int) (availableScrollHeight * touchFraction);

    //Scroll to the desired item. The offset used here is kind of hard to explain.
    //If the position we wish to scroll to is, say, position 10.5, we scroll to position 10,
    //and then offset by 0.5 * rowHeight. This is how we achieve smooth scrolling.
    LinearLayoutManager layoutManager = ((LinearLayoutManager) getLayoutManager());
    layoutManager.scrollToPositionWithOffset(spanCount * exactItemPos / mScrollPosState.rowHeight,
            -(exactItemPos % mScrollPosState.rowHeight));

    if (!(getAdapter() instanceof SectionedAdapter)) {
        return "";
    }

    int posInt = (int) ((touchFraction == 1) ? itemPos - 1 : itemPos);

    SectionedAdapter sectionedAdapter = (SectionedAdapter) getAdapter();
    return sectionedAdapter.getSectionName(posInt);
}
 
Example 18
Source File: ConversationsOverviewFragment.java    From Pix-Art-Messenger with GNU General Public License v3.0 4 votes vote down vote up
private void setScrollPosition(ScrollState scrollPosition) {
    if (scrollPosition != null) {
        LinearLayoutManager layoutManager = (LinearLayoutManager) binding.list.getLayoutManager();
        layoutManager.scrollToPositionWithOffset(scrollPosition.position, scrollPosition.offset);
    }
}
 
Example 19
Source File: CommentListingFragment.java    From RedReader with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onCommentListingRequestAllItemsDownloaded(final ArrayList<RedditCommentListItem> items) {

	mCommentListingManager.addComments(items);

	if(mFloatingToolbar != null && mFloatingToolbar.getVisibility() != View.VISIBLE) {
		mFloatingToolbar.setVisibility(View.VISIBLE);
		final Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.slide_in_from_bottom);
		animation.setInterpolator(new OvershootInterpolator());
		mFloatingToolbar.startAnimation(animation);
	}

	mUrlsToDownload.removeFirst();

	final LinearLayoutManager layoutManager = (LinearLayoutManager)mRecyclerView.getLayoutManager();

	if(mPreviousFirstVisibleItemPosition != null
			&& layoutManager.getItemCount() > mPreviousFirstVisibleItemPosition) {

		layoutManager.scrollToPositionWithOffset(
				mPreviousFirstVisibleItemPosition,
				0);

		mPreviousFirstVisibleItemPosition = null;
	}

	if(mUrlsToDownload.isEmpty()) {

		if(mCommentListingManager.getCommentCount() == 0) {

			final View emptyView = LayoutInflater.from(getContext()).inflate(
					R.layout.no_comments_yet,
					mRecyclerView,
					false);

			if (mCommentListingManager.isSearchListing()) {
				((TextView) emptyView.findViewById(R.id.empty_view_text)).setText(R.string.no_search_results);
			}

			mCommentListingManager.addViewToItems(emptyView);

		} else {

			final View blankView = new View(getContext());
			blankView.setMinimumWidth(1);
			blankView.setMinimumHeight(General.dpToPixels(getContext(), 96));
			mCommentListingManager.addViewToItems(blankView);
		}

		mCommentListingManager.setLoadingVisible(false);

	} else {
		makeNextRequest(getActivity());
	}
}