簡體   English   中英

使用左右按鈕滾動recyclerview

[英]Scrolling recyclerview using buttons right and left

我想使用向右和向左按鈕來滾動具有10個項目的項目

我嘗試使用scrollBy,但是沒有用

這是我的布局圖像。

我想使用向右和向左按鈕滾動 我希望能夠左右滾動

您可以使用一個變量來跟蹤RecyclerView的當前顯示位置,在下面的代碼片段中將其命名為“ currentPosition”

如果要導航到下一個項目,則按以下步驟遞增:

private void scrollToNext() {
    if ((recyclerView.getLayoutManager()) != null) {
        ((LinearLayoutManager) recyclerView.getLayoutManager()).scrollToPositionWithOffset(currentPosition++, 0);
    }
}

您可以使用RelativeLayout或自定義布局代替LinearLayout 根據您在RecyclerView

同樣,如果要上一個減小它

private void scrollToPrevious() {
    if ((recyclerView.getLayoutManager()) != null) {
        ((LinearLayoutManager) recyclerView.getLayoutManager()).scrollToPositionWithOffset(currentPosition--, 0);
    }
}

您可以使用“ 下一步”按鈕:

mRecyclerView.getLayoutManager().scrollToPosition(linearLayoutManager.findLastVisibleItemPosition() + 1);

對於上一個按鈕:

mRecyclerView.getLayoutManager().scrollToPosition(linearLayoutManager.findFirstVisibleItemPosition() - 1);

這個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM