使用ReverseLayout recyclerview将项目添加到Endless Scroll RecyclerView [英] Adding items to Endless Scroll RecyclerView with a ReverseLayout recyclerview

查看:95
本文介绍了使用ReverseLayout recyclerview将项目添加到Endless Scroll RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在普通的回收站视图中,最新的项目都位于顶部,如果您将项目添加到回收站视图中,则会将现有项目下推,而新项目则位于顶部.

In a normal recyclerview, the newest items are all at the top and if you add an item into the recyclerview, it pushes the existing items down and the new item takes the top position.

在reverseLayout recyclerview中,最新的项目都在底部,如果您将一个项目添加到recyclerview中,则会将其添加在底部.这种布局对于评论供稿(您希望最新评论位于底部)之类的东西非常有用.

In a reverseLayout recyclerview, the newest items are all at the bottom and if you add an item into the recyclerview, it is added right at the bottom. This kind of layout is quite useful for things like comment feeds where you expect the latest comments to be at the bottom.

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

A good example of this is the Facebook comment feed when you comment on a friend's status for example. You can see that the comments all have the time that they were posted at the bottom and the times are decreasing from 12 hours to 10 hours from top to bottom:

reverseLayout的设置非常容易,我已经使用以下代码完成了此操作:

The reverseLayout is quite easy to set and I have done it using this code:

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

但是,我正在努力附加一个反向无尽的滚动侦听器.我前一段时间在问题中使用以下Vilen的代码(

I'm however struggling to attach a reverse endless scroll listener. I'm using the following Vilen's code from my question a while back (Adding items to Endless Scroll RecyclerView with ProgressBar at bottom) and modifying it so that it the onLoadMoreListener will only activate itself when it is scrolling up instead of down. You want to scroll up to older comments:

    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 类中调用以下代码:

When the onLoadMoreListener is called, the following code is called in my activity class:

    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.

However, the onLoadMoreListener doesn't seem to be called at all when I scroll up.

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

I also made sure I had at least 10 comments in my feed and 5 was loaded before and 5 more should be loaded when I scroll up.

有什么知道这个反向的OnLoadMoreListener在做什么吗?

Does anything know what I'm doing wrong with this reverse OnLoadMoreListener?

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

The reverseLayout recyclerview actually does work with the original code from Vilen, I have uploaded a github repo to show this:

https://github.com/Winghin2517/ReverseLayoutRecyclerview

尽管激活onLoadMoreListener仍然存在问题-如果注释列表仅包含3条注释,并且没有其他要添加的注释,我不希望当我最初查看我的评论-当我第一次点击我的应用以查看评论时.现在,当我单击以查看我的注释时,即使在数据集中有3条注释,progressBar仍会显示,并且在2秒钟后消失,我认为这是不必要的(请参阅github repo在此处进行演示).

There is still an issue with when the onLoadMoreListener is activated though - If the comment list only comprises of 3 comments and there is no more comments to add on, I don't want the onLoadMoreListener to activate at all when I initially view my comments - when I first click on my app to see the comments. Right now, when I click to view my comments, even though there are 3 comments in the Dataset, the progressBar is still displayed and the disappears after a 2 seconds which I think is not necessary (see github repo where I demonstrate this).

在只有3条注释时禁用它也不是一个好主意,因为onLoadMoreListener就像swipeToRefresh对象一样.如果用户在查看评论提要之后收到来自用户的更多评论,则可以向下滑动,并且onLoadMoreListener将显示这些评论.

Disabling it when there is only 3 comments is also not a good idea as the onLoadMoreListener acts as like a swipeToRefresh object. If there are more comments from the user subsequent to when he views the comment feed, he can swipe down and the onLoadMoreListener will display those comments.

是否有一种方法可以在Feed最初加载时将其禁用?

Is there a way to just disable it when the feed loads initially?

推荐答案

Simon:我之前在此处提出的解决方案具有反向布局,无需任何修改.我唯一要添加的反向布局是在您第一次显示它时自动滚动到第一项,但是这取决于您. 现在回到问题所在.您提到滚动时什么也没发生.所以我的猜测是您以错误的顺序初始化了Recylcer视图.确保您这样做

Simon: solution I previously proposed here works fine with reverse layout without any modification. The only thing I'd add for reverse layout is to scroll automatically to first item when you show it first time but that's up to you. Now getting back to issue you have. You mentioned that when you scroll nothing happens. so my guess is that you initialize your recylcer view in a wrong order. Make sure you do it like this

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

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

注意,首先会实例化布局管理器,然后进行设置,然后提供适配器.让我知道是否是这种情况.

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

Note that layout manager gets instantiated first, then you set it and after that you provide adapter. Let me know if that was the case.

Edit Just bringing up this from comment below:

忘记了onLoadMoreListener的功能,所有滚动内容仅使用包裹在SwipeToRefreshLayout

forget about what we have with onLoadMoreListener and all scrolling stuff just use standard RecyclerView wrapped in SwipeToRefreshLayout

这篇关于使用ReverseLayout recyclerview将项目添加到Endless Scroll RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆