簡體   English   中英

RecyclerView 上 LinearLayoutManager 的 scrollToPositionWithOffset 不起作用

[英]scrollToPositionWithOffset from LinearLayoutManager on RecyclerView not working

我正在嘗試使用RecyclerView制作一個水平的粘性圖像列表,我想使用scrollToPositionWithOffset按像素偏移量移動它們。 我想傳遞0作為position和我想移動到右/左的像素作為offset

但它不起作用,列表保持不變,未滾動,它不會移動。 這是我的實現:

final LargeImageAdapter mLargeImageAdapter = new LargeImageAdapter(this);
linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(mLargeImageAdapter);

seekBar = (SeekBar)findViewById(R.id.seekBar);
seekBar.setMax(7000);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        int scrollToDX = progress;

        ((LinearLayoutManager)recyclerView.getLayoutManager()).scrollToPositionWithOffset(0, scrollToDX);
        // tried invoking also linearLayoutManager instead getLayoutManager.
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
});

我究竟做錯了什么?

非常感謝你。

問候。

拉斐爾。

我終於用了:

recyclerView.scrollBy(int offsetX, int offsetY); 設置offsetY = 0

我不明白函數scrollToPositionWithOffset的用途是什么。

我有一個類似的問題。 我的問題是我的recyclerview的父布局大小不一樣。 我通過將回收站視圖的寬度和高度設置為match_parent解決了這一問題。 我不知道為什么在這種情況下會發生這種情況。

第一個問題的較晚答案,以及答案的補充:

您的方法可以更好地滿足您的個人需求,因為scrollToPositionWithOffset不能滿足您的要求。

正如文檔在這里說的那樣

[...]已解決的布局開始取決於[...] getLayoutDirection(android.view.View)[...]

這意味着它將在您的情況下沿布局方向垂直偏移滾動目標位置。

我不明白函數scrollToPositionWithOffset的用途是什么。

它不僅可以滾動到列表中的給定項目,還可以將其放置在更“可見”或其他方便的位置。

最近我也遇到了這個問題,我在onScrolled()直接調用了scrollToPositionWithOffset ,但沒有任何改變,我轉向 scrollToPosition() 甚至 scrollBy() 但沒有幫助,最后我嘗試延遲它,所以它工作,我第一次延遲50ms,但是兩周后我發現這還不夠,所以我在沒有任何方法的情況下增加到100ms,當然可以,只是感覺有點不安。

val layoutManager = LinearLayoutManager(hostActivity, VERTICAL, false)
fileRv.layoutManager = layoutManager
fileRv.addOnScrollListener(object : RecyclerView.OnScrollListener() {
    override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
        if (dx == 0 && dy == 0) {
            scrollToLastPosition()
        }
    }

    private fun scrollToLastPosition() {
        val lastScrollPosition = viewModel.consumeLastScrollPosition()
        if (lastScrollPosition > 0) {
            Handler().postDelayed({ layoutManager.scrollToPositionWithOffset(lastScrollPosition, 0) }, 100)
        }
    }
})

override fun onItemClick(position: Int) {
    layoutManager.findFirstVisibleItemPosition().let {
        if (it >= 0) viewModel.markLastScrollPosition(it)
    }
}

fun markLastScrollPosition(position: Int) {
    currentFolderListData.value?.lastOrNull()?.lastScrollPosition = position
}

fun consumeLastScrollPosition(): Int {
    currentFolderListData.value?.lastOrNull()?.run {
        return lastScrollPosition.apply { lastScrollPosition = -1 }
    }
    return 0
}

我找到了解決辦法。 因為我是 DNA Launcher 的開發者。 當我使用RecyclerView顯示AZ App List時,發現function scrollToPositionWithOffset不工作了。 我跟蹤了將近一天的問題,然后我弄明白了。

當RecyclerView再次顯示時,只要讓RecyclerView的parent做requestLayout即可。

這個對我有用。

而且我知道如何使 function scrollToPositionWithOffset 不起作用。 你只需要在它上面添加一個視圖然后讓它消失。

暫無
暫無

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

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