Recyclerview 完全滚动到位置

Recyclerview fully scroll to position

我的 recyclerview 应该打开第一个关闭的项目。为此,我写下了这几行。

 LinearLayoutManager llm = new LinearLayoutManager(getActivity());
 llm.scrollToPosition(1);
 paletteRecyclerView.setLayoutManager(llm);

我想检查是否可以完全滚动到位置 1。如果我的 recyclerview 很小,我必须在不关闭第一项的情况下打开它。我该如何检查?

试试下面的代码...

LinearLayoutManager llm = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(llm);

RecyclerView.Adapter adapter = new YourAdapter();
recyclerView.setAdapter(adapter);

recyclerView.scrollToPosition(position);

我假设您想知道 RecyclerView 中是否有要滚动的项目。因此,您可以在将适配器设置为 recyclerView 后进行如下检查。

recyclerView.post(new Runnable() {
        @Override
        public void run() {
            int recyclerViewheight = recyclerView.getHeight();
            int totalChildViewsHeight = recyclerView.computeVerticalScrollRange();
            if (recyclerViewheight > totalChildViewsHeight) {
                //there is no scroll (there are no more items to scroll)
            } else {
                //there is scroll (there are items to scroll)
            }
        }
    });