在普通的回收器视图中,最新的项目都位于顶部,如果您将项目添加到回收器视图中,它会将现有项目向下推,新项目占据顶部位置。
在reverseLayout recyclerview中,最新的项目都位于底部,如果您将项目添加到recyclerview中,它将添加到底部。这种布局对于评论源之类的东西非常有用,您希望最新的评论位于底部。
一个很好的例子是 Facebook 评论源,例如当您评论朋友的状态时。可以看到,评论的发布时间都在最下方,从上到下时间从12小时递减到10小时:
verseLayout 很容易设置,我已经使用以下代码完成了它:
mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
然而,我正在努力附加一个反向无限滚动监听器。我不久前在我的问题中使用了以下 Vilen 的代码(将项目添加到 Endless Scroll RecyclerView 中,进度条位于底部 https://stackoverflow.com/questions/30681905/adding-items-to-endless-scroll-recyclerview-with-progressbar-at-bottom)并修改它,以便 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 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 似乎根本没有被调用。
我还确保我的提要中至少有 10 条评论,并且之前已加载 5 条评论,当我向上滚动时,还应该加载 5 条评论。
有谁知道我这个反向 OnLoadMoreListener 做错了什么?
EDIT:
verseLayout recyclerview 实际上可以与 Vilen 的原始代码一起使用,我已经上传了一个 github 存储库来展示这一点:
https://github.com/Winghin2517/ReverseLayoutRecyclerview https://github.com/Winghin2517/ReverseLayoutRecyclerview
不过,激活 onLoadMoreListener 时仍然存在问题 - 如果评论列表仅包含 3 条评论,并且没有更多评论可添加,那么我根本不希望 onLoadMoreListener 激活最初查看我的评论 - 当我first点击我的应用程序即可查看评论。现在,当我点击查看我的评论时,即使数据集中有 3 条评论,进度条仍然显示,并在 2 秒后消失,我认为这是没有必要的(请参阅 github 存储库,我在其中演示了这一点)。
当只有 3 条评论时禁用它也不是一个好主意,因为 onLoadMoreListener 的作用类似于 swipeToRefresh 对象。如果用户在查看评论源后有更多评论,他可以向下滑动,onLoadMoreListener 将显示这些评论。
有没有办法在 feed 加载时禁用它最初?