Android: 在初始化时滚动到顶部的StaggeredGridLayoutManager

8
我正在使用StaggeredGridLayoutManager来制作我的图片库。我已经设置了setReverseLayout(true),所以图像从底部堆叠。
我遇到的问题是,在应用程序初始化时,滚动条定位在图库底部。当用户第一次启动应用程序时,我希望滚动条位于图库顶部。
我尝试使用scrollToPosition(以下代码片段),但滚动条最终停留在图库中间某个位置,可能是因为在调用scrollToPosition时图像还没有完全加载完成。
mLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
    mLayoutManager.setReverseLayout(true);

mRecyclerView.setLayoutManager(mLayoutManager);
mImageList = ((MainActivity)getActivity()).getImages();
adapter = new ImageAdapter(getActivity(),mImageList);
mRecyclerView.setAdapter(adapter);
mRecyclerView.scrollToPosition(mImageList.size()-1);

有没有一种正确的方法将滚动条指向顶部,而不是底部?

5个回答

3

我之前解决过类似的问题,这里是方法:

mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @SuppressWarnings("deprecation")
                    @Override
                    public void onGlobalLayout() {
                        ViewTreeObserver observer = mRecyclerView.getViewTreeObserver();
                        scrollRecyclerViewToTop();
                        if (!observer.isAlive()) {
                            return;
                        }
                        if (mScrolledByUser) {
                            if (hasJellyBeanApi()) {
                                observer.removeOnGlobalLayoutListener(this);
                            } else {
                                observer.removeGlobalOnLayoutListener(this);
                            }
                        }
                    }
                });

并且:

mRecyclerView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        mScrollByUser = true;
        return false;
    }
});

并且:

private void scrollRecyclerViewToTop() {
    mRecyclerView.scrollToPosition(0);
    mRecyclerView.scrollBy(0, Integer.MIN_VALUE);
}

意思很简单:让RecyclerView始终滚动到顶部,直到用户触摸它。希望这可以帮到您。

只有当我触摸屏幕时才能工作,如果我不触摸屏幕就没有任何显示。我想默认滚动,这样用户就不需要触摸。这是最接近的答案,所以我会为您奖励悬赏。顺便说一句,我的答案可以按照我想要的方式工作。 - ugur

1

那不会完全解决问题。第一个可见的项并不总是最后一项。因此,在不同的运行中我可能会滚动到不同的位置。 - Neo

1
尝试使用StaggeredGridLayoutManagerscrollToPositionWithOffset()函数,而不是scrollToPosition。我发现它比scrollToPosition更可靠。同时确保在Handler().post()内执行此函数。这将导致Runnable被添加到消息队列中。它会在UI线程空闲时运行。
new Handler().post(new Runnable() {
            @Override
            public void run() {
                staggeredGridLayoutManager.scrollToPositionWithOffset(mImageList.size() - 1, 0);
            }
        });

很遗憾,这对我没有起作用。感谢您的回答。 - ugur

1
我在DataObserver中使用了scrollToPosition,现在它可以正常工作了。
当RecyclerView加载时,以下代码可以显示顶部的项目:
rcAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onItemRangeInserted(int positionStart, int itemCount) {
                super.onItemRangeInserted(positionStart, itemCount);
                int count = rcAdapter.getItemCount();
                mRecyclerView.scrollToPosition(itemCount-1);

            }
        });

若想像聊天视图一样滚动到底部查看新添加的项目,请使用以下代码。

     rcAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
                    @Override
                    public void onItemRangeInserted(int positionStart, int itemCount) {
                        super.onItemRangeInserted(positionStart, itemCount);
                        int count = rcAdapter.getItemCount();
                        int lastVisiblePositions[] = new int[2]; //for 2 columns
                        int lastVisiblePosition = staggeredGridLayoutManager.findLastCompletelyVisibleItemPositions(lastVisiblePositions)[0];
                        // If the recycler view is initially being loaded or the user is at the bottom of the list, scroll
                        // to the bottom of the list to show the newly added message.
                        if (lastVisiblePosition == -1 ||
                            (positionStart >= (count- 1) && lastVisiblePosition == (positionStart - 1))) {
                           mRecyclerView.scrollToPosition(positionStart);
                        }

                    }
                });

看起来很有趣。 - Neo

0

这段代码可能会对你有所帮助,我在代码中也使用了这个链接。

https://guides.codepath.com/android/Endless-Scrolling-with-AdapterViews-and-RecyclerView

每个AdapterView(例如ListView和GridView)都支持绑定到OnScrollListener事件,该事件在用户滚动浏览集合时触发。使用此系统,我们可以定义一个基本的EndlessScrollListener,通过创建扩展OnScrollListener的自定义类来支持大多数用例:
public abstract class EndlessScrollListener implements AbsListView.OnScrollListener {
    // The minimum number of items to have below your current scroll position
    // before loading more.
    private int visibleThreshold = 5;
    // The current offset index of data you have loaded
    private int currentPage = 0;
    // The total number of items in the dataset after the last load
    private int previousTotalItemCount = 0;
    // True if we are still waiting for the last set of data to load.
    private boolean loading = true;
    // Sets the starting page index
    private int startingPageIndex = 0;

    public EndlessScrollListener() {
    }

    public EndlessScrollListener(int visibleThreshold) {
        this.visibleThreshold = visibleThreshold;
    }

    public EndlessScrollListener(int visibleThreshold, int startPage) {
        this.visibleThreshold = visibleThreshold;
        this.startingPageIndex = startPage;
        this.currentPage = startPage;
    }

    // This happens many times a second during a scroll, so be wary of the code you place here.
    // We are given a few useful parameters to help us work out if we need to load some more data,
    // but first we check if we are waiting for the previous load to finish.
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) 
  {
        // If the total item count is zero and the previous isn't, assume the
        // list is invalidated and should be reset back to initial state
        if (totalItemCount < previousTotalItemCount) {
            this.currentPage = this.startingPageIndex;
            this.previousTotalItemCount = totalItemCount;
            if (totalItemCount == 0) { this.loading = true; } 
        }
        // If it's still loading, we check to see if the dataset count has
        // changed, if so we conclude it has finished loading and update the current page
        // number and total item count.
        if (loading && (totalItemCount > previousTotalItemCount)) {
            loading = false;
            previousTotalItemCount = totalItemCount;
            currentPage++;
        }

        // If it isn't currently loading, we check to see if we have breached
        // the visibleThreshold and need to reload more data.
        // If we do need to reload some more data, we execute onLoadMore to fetch the data.
        if (!loading && (firstVisibleItem + visibleItemCount + visibleThreshold) >= totalItemCount ) {
         loading = onLoadMore(currentPage + 1, totalItemCount);
        }
    }

    // Defines the process for actually loading more data based on page
    // Returns true if more data is being loaded; returns false if there is no more data to load.
    public abstract boolean onLoadMore(int page, int totalItemsCount);

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // Don't take any action on changed
    }
  }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接