当前位置:  开发笔记 > 编程语言 > 正文

使用ReverseLayout recyclerview将项目添加到Endless Scroll RecyclerView

如何解决《使用ReverseLayoutrecyclerview将项目添加到EndlessScrollRecyclerView》经验,为你挑选了1个好方法。

在正常的recyclerview中,最新的项目都在顶部,如果您将项目添加到recyclerview中,它会将现有项目向下推,新项目将位于顶部位置.

在reverseLayout recyclerview中,最新的项目都在底部,如果您将项目添加到recyclerview中,则会在底部添加.这种布局对于评论提要非常有用,您希望最新评论位于底部.

例如,当您评论朋友的状态时,Facebook评论供稿就是一个很好的例子.您可以看到评论都有时间在底部发布,时间从上到下从12小时减少到10小时:

facebook评论Feed

reverseLayout很容易设置,我使用这段代码完成了它:

mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);

然而,我正在努力附加一个反向无限滚动监听器.我使用下面的维伦的代码,我的问题了一段时间后(在底部带有进度添加项目到无尽的滚动RecyclerView),然后修改它,使它的onLoadMoreListener只会激活自己当它向下,而不是向上滚动.您想要向上滚动到较旧的评论:

    if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
        final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
        //if you add on addOnScrollListener, it will cause the scroll listener to be called twice each time you reset your adapter
        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            int firstVisibleItem;
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                final int currentFirstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
                totalItemCount = linearLayoutManager.getItemCount();
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();

                if(lastVisibleItem > firstVisibleItem)
                {
                    Log.i("SCROLLING UP","TRUE");
                    if (!loading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
                        current_page++;
                        if (onLoadMoreListener != null) {
                            onLoadMoreListener.onLoadMore(current_page);
                        }
                        loading = true;
                    }
                }
                lastVisibleItem=firstVisibleItem;
            }
        });
    }

调用onLoadMoreListener时,在我的activity类中调用以下代码:

    CommentsAdapter.OnLoadMoreListener onLoadMoreListener = new CommentsAdapter.OnLoadMoreListener() {
    @Override
    public void onLoadMore(int current_page) {
        //get the firstCommentId on the list of comments so that we can send it to the server to fetch comments less than that comment id to display in our recyclerview
        firstCommentId = comments.get(0).getId();
        //add empty item for progress bar / wheel and display the progress bar / wheel
        Comment comment = new Comment();
        comment.setViewType(Constants.COMMENT_PROGRESS_BAR);
        comments.add(0, comment);
        mCommentAdapter.notifyItemInserted(0);
        mCommentAdapter.notifyItemRangeChanged(1, comments.size());

        //CODE FOR NETWORK OPERATIONS GOES HERE TO FETCH THE NEXT FEW COMMENTS WHEN SCROLLING UP//
    }
};

但是,当我向上滚动时,似乎根本没有调用onLoadMoreListener.

我还确保我的Feed中至少有10条评论,之前加载了5条评论,当我向上滚动时还应加载5条评论.

有什么事情知道我在使用这个反向OnLoadMoreListener做错了吗?

编辑:

reverseLayout recyclerview实际上可以使用Vilen的原始代码,我已经上传了一个github repo来显示:

https://github.com/Winghin2517/ReverseLayoutRecyclerview

还有与当onLoadMoreListener被激活,虽然一个问题-如果评论列表只包括3条评论,也没有更多的评论上添加,我不希望onLoadMoreListener激活在所有的时候我开始查看我的意见-当我第一次点击我的应用程序查看评论时.现在,当我点击查看我的评论时,即使数据集中有3个评论,仍然会显示progressBar,并且在2秒后消失,我认为没有必要(请参阅github repo,我在此演示).

当只有3个注释时禁用它也不是一个好主意,因为onLoadMoreListener就像swipeToRefresh对象一样.如果用户在查看评论供稿后有更多评论,他可以向下滑动,onLoadMoreListener将显示这些评论.

有没有办法在最初加载Feed时禁用它?



1> Vilen..:

西蒙:我之前提出的解决方案可以在没有任何修改的情 我为反向布局添加的唯一内容是在第一次显示时自动滚动到第一个项目,但这取决于您.现在回过头来发帖了.你提到滚动时没有任何反应.所以我的猜测是你以错误的顺序初始化你的recylcer视图.确保你这样做

mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);

mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new MyAdapter<>(myDataset, mRecyclerView);
mRecyclerView.setAdapter(mAdapter);

请注意,布局管理器首先进行实例化,然后设置它,然后提供适配器.如果是这样,请告诉我.

编辑只需从下面的评论中提出:

忘记我们所拥有的onLoadMoreListener和所有滚动的东西只是使用标准RecyclerView包装SwipeToRefreshLayout

推荐阅读
wurtjq
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有