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);

是否有正确的方法将滚动条指向顶部而不是底部?

4

5 回答 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始终滚动到顶部,直到用户触摸它。希望这可以帮到你。

于 2016-11-14T08:22:07.033 回答
1

尝试通过调用findFirstCompletelyVisibleItemPositions找到第一个可见位置

findFirstVisibleItemPositions

使用从先前方法获得的位置调用您的回收站视图 scrollToPosition

于 2016-03-07T13:02:43.003 回答
1

尝试改用scrollToPositionWithOffset()of 函数StaggeredGridLayoutManager。我发现它比scrollToPosition. 还要确保在Handler().post(). 这将导致Runnable 被添加到消息队列中。一旦 UI 线程空闲,它将运行。

new Handler().post(new Runnable() {
            @Override
            public void run() {
                staggeredGridLayoutManager.scrollToPositionWithOffset(mImageList.size() - 1, 0);
            }
        });
于 2016-11-09T05:32:42.450 回答
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);
                        }

                    }
                });
于 2016-11-09T18:11:43.677 回答
0

这段代码可以帮助你,我也在代码中使用了这个链接

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

每个 AdapterView(例如 ListView 和 GridView)都支持绑定到 OnScrollListener 事件,这些事件在用户滚动浏览集合时触发。使用这个系统,我们可以通过创建我们自己的扩展 OnScrollListener 的类来定义一个支持大多数用例的基本 EndlessScrollListener:

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
    }
  }
于 2016-11-15T17:06:35.017 回答