用viewType的方法做区分
制作加载条layout,改progressbar颜色如下,需要API21以上
adpater重写区分viewtype
static final int VIEW_TIPE_NORMAL = 0;
static final int VIEW_TIPE_BOTTOM = 1;
@Override
public int getItemCount() {
return super.getItemCount() + 1;
}
@Override
public int getItemViewType(int position) {
return position == getItemCount() - 1 ? VIEW_TIPE_BOTTOM : VIEW_TIPE_NORMAL;
}
3 adpater中修改onCreateViewHolder
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, int viewType) {
final MyViewHolder myViewHolder;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
if (viewType == VIEW_TIPE_NORMAL){
// 原来的myViewHolder创建方法
}else {
final View itemView = inflater.inflate(R.layout.footer, parent, false);
myViewHolder = new MyViewHolder(itemView);
}
return myViewHolder;
}
4 onBindViewHolder添加:
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
if (getItemCount() - 1 == position) {
return;
}
}
监听加载:(还有很多相似方法,这里就列举一个)
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy < 0 ){return;}
GridLayoutManager layoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
int p = layoutManager.findLastVisibleItemPosition();
if (p == myAdapter.getItemCount() - 1){
Log.i("test","到底了");
//加载功能性代码
}
}
});
加载记得做判断防止多次连续请求