首先使用RecyclerView的adapter继承:RecyclerView.Adapter
public class OrderListAdapter extends RecyclerView.Adapter {
//TODO
}
2、创建ViewHolder 需要继承
class ViewHolder extends RecyclerView.ViewHolder{
//TODO 声明view
public ViewHolder(View convertView) {
super(convertView);
//TODO 初始化view
}
}
通过onCreateViewHolder 返回好创建的布局文件:
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(mContext).inflate(R.layout.xxxx, parent, false);
return new ViewHolder(inflate);
}
接下来重点来了:
通过onBindViewHolder 带List payloads 参数的方法来区分是现实列表中的同一位置的view的刷新:
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, List payloads) {
if (null == payloads || payloads.size() < 1) {
onBindViewHolder(holder, position);
} else if (mIsActivityTop) {
for (Object payload : payloads) {
// 刷新倒计时
if (payload instanceof Integer && ((int) payload) == UPDATE_COUNT_DOWN_TIME) {
((ViewHolder) holder).tv_time.setText("刷新了"));
}
}
}
}
传入的整型:
private final static int UPDATE_COUNT_DOWN_TIME = 1;
而payloads可以List,这样就可以区分刷新不同区域的view了.
而上面的调用的onBindViewHolder(holder, position),就是重写的onBindViewHolder(holder, position)的方法了:
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//TODO 写绑定view 的逻辑以及显示
}
对于什么时候会调用到onBindViewHolder(holder, position, payloads)的方法:
当你写倒计时的时候:(倒计时是用rxjava写的,在onNext的时候)
Subscription adSkipTimer Observable.interval(1, TimeUnit.SECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Long>() {
@Override
public void onCompleted() { } @Override
public void onError(Throwable e) { } @Override
public void onNext(Long aLong) {
notifyItemRangeChanged(0, getItemCount(), UPDATE_COUNT_DOWN_TIME);
}
}
});
通过 notifyItemRangeChanged 来调用onBindViewHolder(holder, position, payloads)的方法。
通过notifyDataSetChanged 来调用onBindViewHolder(holder, position)的方法。