【发布时间】:2016-03-25 03:19:24
【问题描述】:
在正常的回收视图中,最新的项目都在顶部,如果你将一个项目添加到回收视图中,它会将现有的项目向下推,新的项目占据顶部的位置。
在reverseLayout recyclerview 中,最新的项目都在底部,如果你将一个项目添加到recyclerview 中,它会被添加到底部。这种布局对于您希望最新的 cmets 位于底部的评论提要等内容非常有用。
例如,当您对朋友的状态发表评论时,Facebook 评论提要就是一个很好的例子。可以看到cmets都有贴在底部的时间,时间从上到下从12小时减少到10小时:
reverseLayout 很容易设置,我使用以下代码完成了它:
mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
然而,我正在努力附加一个反向无限滚动监听器。我正在使用我的问题中的以下 Vilen 代码(Adding items to Endless Scroll RecyclerView with ProgressBar at bottom)并对其进行修改,以便 onLoadMoreListener 仅在向上滚动而不是向下滚动时才会激活。您想向上滚动到较旧的 cmets:
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。
我还确保我的提要中至少有 10 个 cmets,之前加载了 5 个,当我向上滚动时应该再加载 5 个。
有没有人知道我在使用这个反向 OnLoadMoreListener 时做错了什么?
编辑:
reverseLayout recyclerview 实际上可以使用 Vilen 的原始代码,我已经上传了一个 github repo 来展示这一点:
https://github.com/Winghin2517/ReverseLayoutRecyclerview
当 onLoadMoreListener 被激活时仍然存在问题 - 如果评论列表仅包含 3 个 cmets 并且没有更多 cmets 可以添加,我不希望 onLoadMoreListener 在我 最初查看我的 cmets - 当我第一次点击我的应用查看 cmets 时。现在,当我单击查看我的 cmets 时,即使 Dataset 中有 3 个 cmets,progressBar 仍会显示,并且在 2 秒后消失,我认为这是不必要的(请参阅我演示的 github repo)。
当只有 3 个 cmets 时禁用它也不是一个好主意,因为 onLoadMoreListener 就像一个 swipeToRefresh 对象。如果在用户查看评论提要之后有更多的 cmets,他可以向下滑动,onLoadMoreListener 将显示这些 cmets。
有没有办法在提要加载最初时禁用它?
【问题讨论】:
-
嗨,Simon,我有几分钟空闲时间会去看看。平均时间确保您没有使用最后一个可见项目,而是在当前可见项目位置为 1 或 0 时触发刷新
-
谢谢维伦。我一直在尝试不同的东西,但不幸的是我仍然无法让它正常工作。我已经增加了一笔赏金来增加交易。
标签: android android-recyclerview onscrolllistener