Android 在Recyclerview中使用滑动刷新方法加载上一页注释
我想知道如何在recyclerview上使用swipe to refresh方法加载上一页的项目。似乎在任何地方都找不到解决办法。我创建了一个应用程序,其中包含一个无休止的Recyclerview,其中包含来自在线论坛的数据。当用户一直向下滚动到底部时,它将加载下一页并填充recyclerview上的下一条注释。它工作得很好 现在我想实现同样的目标,但是当用户滚动到顶部时,加载上一页。我希望用户执行一次滑动,这将加载上一页,并使用顶部的上一条注释填充recyclerview。我该怎么做 MainActivity.javaAndroid 在Recyclerview中使用滑动刷新方法加载上一页注释,android,android-recyclerview,Android,Android Recyclerview,我想知道如何在recyclerview上使用swipe to refresh方法加载上一页的项目。似乎在任何地方都找不到解决办法。我创建了一个应用程序,其中包含一个无休止的Recyclerview,其中包含来自在线论坛的数据。当用户一直向下滚动到底部时,它将加载下一页并填充recyclerview上的下一条注释。它工作得很好 现在我想实现同样的目标,但是当用户滚动到顶部时,加载上一页。我希望用户执行一次滑动,这将加载上一页,并使用顶部的上一条注释填充recyclerview。我该怎么做 Main
private ArrayList<CommentItem> commentsList = new ArrayList<CommentItem>();
private RecyclerAdapter adapter_thread;
private int pageNumber = 1;
//recyclerview initialization have been done here
adapter_replies.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore() {
commentsList.add(null); //to show progressbar at the bottom while loading the next page
adapter_thread.notifyItemInserted(commentsList.size() - 1);
++pageNumber;
getComments(); //async task load next page comments
}
});
private boolean loading;
private OnLoadMoreListener onLoadMoreListener;
private int visibleThreshold = 1;
public RecyclerAdapter(Context context, final ArrayList<CommentItem> commentsList, RecyclerView recyclerView) {
this.commentsList= commentsList;
this.context = context;
if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView
.getLayoutManager();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView,
int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount = linearLayoutManager.getItemCount();
lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
verylastVisibleItem = linearLayoutManager.findLastCompletelyVisibleItemPosition();
if (!loading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
if (onLoadMoreListener != null) {
onLoadMoreListener.onLoadMore();
}
loading = true;
}
}
});
}
private ArrayList commentsList=new ArrayList();
专用回收适配器\u线程;
私有int pageNumber=1;
//recyclerview初始化已在此处完成
adapter_responses.setOnLoadMoreListener(新的OnLoadMoreListener(){
@凌驾
public void onLoadMore(){
commentsList.add(null);//加载下一页时在底部显示progressbar
适配器\u thread.notifyItemInserted(commentsList.size()-1);
++页码;
getComments();//异步任务加载下一页注释
}
});
RecyclerAdapter.java
private ArrayList<CommentItem> commentsList = new ArrayList<CommentItem>();
private RecyclerAdapter adapter_thread;
private int pageNumber = 1;
//recyclerview initialization have been done here
adapter_replies.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore() {
commentsList.add(null); //to show progressbar at the bottom while loading the next page
adapter_thread.notifyItemInserted(commentsList.size() - 1);
++pageNumber;
getComments(); //async task load next page comments
}
});
private boolean loading;
private OnLoadMoreListener onLoadMoreListener;
private int visibleThreshold = 1;
public RecyclerAdapter(Context context, final ArrayList<CommentItem> commentsList, RecyclerView recyclerView) {
this.commentsList= commentsList;
this.context = context;
if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView
.getLayoutManager();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView,
int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount = linearLayoutManager.getItemCount();
lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
verylastVisibleItem = linearLayoutManager.findLastCompletelyVisibleItemPosition();
if (!loading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
if (onLoadMoreListener != null) {
onLoadMoreListener.onLoadMore();
}
loading = true;
}
}
});
}
私有布尔加载;
私有OnLoadMoreListener OnLoadMoreListener;
私有int visibleThreshold=1;
公共RecyclerAdapter(上下文上下文、最终ArrayList注释列表、RecyclerView RecyclerView){
this.commentsList=commentsList;
this.context=上下文;
if(recyclerView.getLayoutManager()实例的LinearLayoutManager){
最终LinearLayoutManager LinearLayoutManager=(LinearLayoutManager)回收视图
.getLayoutManager();
recyclerView.addOnScrollListener(新的recyclerView.OnScrollListener(){
@凌驾
已填空的公共空间(RecyclerView RecyclerView,
整数dx,整数dy){
super.onScrolled(recyclerView、dx、dy);
totalItemCount=linearLayoutManager.getItemCount();
lastVisibleItem=linearLayoutManager.findLastVisibleItemPosition();
firstVisibleItem=linearLayoutManager.findFirstVisibleItemPosition();
verylastVisibleItem=linearLayoutManager.findLastCompletelyVisibleItemPosition();
如果(!loading&&totalItemCount您可以使用Hi,谢谢我正在使用它。实际上,我关心的是我不确定如何将上一页的注释添加到当前列表的顶部。以及如何在加载适配器后通知适配器。创建数组列表将其传递给适配器,然后将注释列表添加到ar中您已传递给适配器并调用适配器的raylit.notifyDatasetChanged()若要按相反顺序显示它们,您可以反转arraylist或将LinearLayoutManager反向布局设置为true,给定的链接将帮助您