这篇文章参考:

https://www.journaldev.com/24041/android-recyclerview-load-more-endless-scrolling

这一篇文章讲的是RecyclerView的endless scrolling

我们都见过APP向上滑动然后又一个旋转小圈圈(ProgressBar)在那转呀转,转完之后就加载出更多的Item。是不是很神奇,那接下来我们讲解一下是如何实现的吧。

其实就是在RecyclerView的Adapter中的数据末尾加上null,通过null来作为提示显示圆圈和加载更多。

首先是旋转小圆圈,我们在Resource的layout文件夹中创建circle_wait.xml, root为linearLayout:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/progress_linear_layout"
android:orientation="vertical">

<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/upload_circle"
android:layout_gravity="center_horizontal"/>

</LinearLayout>

RecyclerView其中包含的View的Layout我就掠过了

然后我们需要在RecyclerView中的Adapter中加上null的情况:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.journaldev.androidrecyclerviewloadmore;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.List;

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

//两种情况
//第一种,正常的Item,即VIEW_TYPE_ITEM
//第二种, null的Item,代表正在加载中即VIEW_TYPE_LOADING
private final int VIEW_TYPE_ITEM = 0;
private final int VIEW_TYPE_LOADING = 1;

//这个是Adapter绑定的数据
public List<String> mItemList;


//构造函数传入数据
public RecyclerViewAdapter(List<String> itemList) {

mItemList = itemList;
}


//RecyclerView创建ViewHolder时调用的函数,用来返回Viewholder
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_ITEM) { //正常的Item
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row, parent, false);
return new ItemViewHolder(view); //返回正常的View holder
} else { //否则返回ProgressBar的ViewHolder
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_loading, parent, false);
return new LoadingViewHolder(view);
}
}

//为ViewHolder中的View绑定数据
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {

if (viewHolder instanceof ItemViewHolder) { //如果是正常的Item,正常绑定数据
populateItemRows((ItemViewHolder) viewHolder, position);
} else if (viewHolder instanceof LoadingViewHolder) { //如果是ProgressBar的Loading界面,什么都不做
showLoadingView((LoadingViewHolder) viewHolder, position);
}

}

//RecyclerView用来控制显示View的多少
@Override
public int getItemCount() {
return mItemList == null ? 0 : mItemList.size();
}

/**
* The following method decides the type of ViewHolder to display in the RecyclerView
* 下面就两种Item(正常和null)的ViewHolder定义
* @param position
* @return
*/
//我们在onCreateViewHolder的时候判断的类型,其实是在这里给的
@Override
public int getItemViewType(int position) {
return mItemList.get(position) == null ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM;
}


//普通Item的ViewHolder
private class ItemViewHolder extends RecyclerView.ViewHolder {

TextView tvItem;

public ItemViewHolder(@NonNull View itemView) {
super(itemView);

tvItem = itemView.findViewById(R.id.tvItem);
}
}

//null的item的ViewHolder
private class LoadingViewHolder extends RecyclerView.ViewHolder {

ProgressBar progressBar;

public LoadingViewHolder(@NonNull View itemView) {
super(itemView);
progressBar = itemView.findViewById(R.id.progressBar);
}
}

//如果是null的item的绑定,我们什么都不做,留空就好,因为我们只需要将ProgressBar显示在界面上就ok
private void showLoadingView(LoadingViewHolder viewHolder, int position) {
//ProgressBar would be displayed

}

//普通Item的绑定
private void populateItemRows(ItemViewHolder viewHolder, int position) {

String item = mItemList.get(position);
viewHolder.tvItem.setText(item);

}

//获取绑定在Adapter中的数据,方便再Listener中使用
public List<String> getListFromAdapter(int index) {
return mItemList;
}


}

接下来就是MainActivity里面,将Adapter绑定到RecyclerView上,并且为RecyclerView添加上Scroll Listner:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

package com.journaldev.androidrecyclerviewloadmore;

import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

RecyclerView recyclerView;
RecyclerViewAdapter recyclerViewAdapter;
ArrayList<String> rowsArrayList = new ArrayList<>();

boolean isLoading = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//找到recyclerView
recyclerView = findViewById(R.id.recyclerView);
//填充数据
populateData();
//绑定Adapter
initAdapter();
//绑定Scroll Listener
initScrollListener();


}

//填充数据
private void populateData() {
int i = 0;
while (i < 10) {
rowsArrayList.add("Item " + i);
i++;
}
}

//初始化Adapter,为其绑定上数据,再将其绑定到recyclerView上
private void initAdapter() {

recyclerViewAdapter = new RecyclerViewAdapter(rowsArrayList);
recyclerView.setAdapter(recyclerViewAdapter);
}

//初始化Scroll Listener
private void initScrollListener() {
//为recyclerView加上Scroll Listener
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}

//我们实现这个方法,因为我们要Listen合适滑倒最底下
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);

//从recyclerView中获取LinearManager
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();

//因为一直再监听,加上条件,只监听一次。只添加一个progress bar
if (!isLoading) {
if (linearLayoutManager != null && linearLayoutManager.findLastCompletelyVisibleItemPosition() == rowsArrayList.size() - 1) { //当当前数据全部划完
//bottom of list!
//从Adapter中获取数据
rowsArrayList = recyclerView.getListFromAdapter();
//在数据最尾部加上null
rowsArrayList.add(null);
//提示recycler view插入了一个数据了,请更新数据吧
//更新完后屏幕上便显示了progress bar
recyclerView.getAdapter().notifyItemInserted(rowsArrayList.size() - 1);
isLoading = true; //设置为true后便不会一直add null,出现很多个progress bar

//这里再扩充你的数据

//扩充完毕后用recyclerView中的adapter notify dataset的变化
//例如: recyclerView.getAdapter().notifyDataSetChanged();

//最后loading完毕后把isLoading变为false便又可以load更多data
isLoading = false;
}
}
}
});


}

}