(Smooth)ScrollToPosition doesn't work properly

2019-01-14 10:41发布

I'm using basic RecyclerView with GridLayoutManager. I observed that nor smoothScrollToPosition nor scrollToPosition works properly.

a) when using smoothScrollToPosition I often receive error from RecyclerView

"RecyclerView﹕ Passed over target position while smooth scrolling."

and RecyclerView is not scrolled properly (often it misses the targeted row). This is observed mostly when I'm trying to scroll to the 1st item of some row

b) when using scrollToPosition it seems to work quite ok but most of the time I can see only the 1st item of the row and the rest are not displayed.

Can you give me some hints how to make work properly at least one of the methods?

Thanks a lot!

8条回答
狗以群分
2楼-- · 2019-01-14 11:22

To scroll down to bottom from any position in the RecyclerView on clicking EditText.

edittext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rv_commentList.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                      rv_commentList.scrollToPosition(rv_commentList.getAdapter().getItemCount() - 1);
                    }
                }, 1000);
            }
        });
查看更多
Fickle 薄情
3楼-- · 2019-01-14 11:22

If you are trying to do a quick scroll to a position at the top of the RecyclerView, just use LinearLayoutManager.scrollToPositionWithOffset with 0 as the offset.

Example:

mLinearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLinearLayoutManager);

mLinearLayoutManager.scrollToPositionWithOffset(myPosition, 0);

smoothScrollToPosition is very slow. If you want something fast go with scrollToPositionWithOffset.

查看更多
登录 后发表回答