在项目中,有数据需要用RecyclerVeiw进行分页显示,类似于,若不是最后一页,需要在最后一个item中显示“正在加载新数据请稍后”,否则显示“无更多数据”,这种效果。
主要修改还是在RecyclerVeiw的Adapter中。
需要重写getItemViewType(int position)这个方法。并且在onBindViewHolder(RecyclerView.ViewHolder holder, int position)这个方法中对于Type进行判断,由此显示不同布局。
直接上代码。
Adapter代码如下:
public class WssbjlcxRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int ITEM_TYPE_FOOTER = 5;
private String strFooter = "";
private TextView tvFooter;
public WssbjlcxRvAdapter(Context context, ArrayList<WssbjlcxBean.ResultBean> list) {
this.context = context;
this.list = list;
}
private Context context;
private ArrayList<WssbjlcxBean.ResultBean> list;
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == ITEM_TYPE_FOOTER) {
FooterViewHolder holder = new FooterViewHolder(LayoutInflater.from(
context).inflate(R.layout.item_wssbjlcx_rv_footer, parent,
false));
return holder;
}
MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
context).inflate(R.layout.item_wssbjlcx_rv, parent,
false));
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (getItemViewType(position) != ITEM_TYPE_FOOTER) {
MyViewHolder mHolder = (MyViewHolder) holder;
mHolder.tvWssbjlcxYzpjxh.setText(list.get(position).getYzpzxh());
mHolder.tvWssbjlcxBblx.setText(list.get(position).getBdmc());
mHolder.tvWssbjlcxSsqq.setText(list.get(position).getSsqq());
mHolder.tvWssbjlcxSsqz.setText(list.get(position).getSsqz());
mHolder.tvWssbjlcxSbrq.setText(list.get(position).getSbrq());
mHolder.tvWssbjlcxSbse.setText("" + list.get(position).getYbtse());
} else {
FooterViewHolder mHolder = (FooterViewHolder) holder;
tvFooter = mHolder.tvFooter;
tvFooter.setText(strFooter);
}
}
@Override
public int getItemViewType(int position) {
if (position == getItemCount() - 1) {
return ITEM_TYPE_FOOTER;
} else {
return 1;
}
}
@Override
public int getItemCount() {
return list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.tv_wssbjlcx_yzpjxh)
TextView tvWssbjlcxYzpjxh;
@BindView(R.id.tv_wssbjlcx_bblx)
TextView tvWssbjlcxBblx;
@BindView(R.id.tv_wssbjlcx_ssqq)
TextView tvWssbjlcxSsqq;
@BindView(R.id.tv_wssbjlcx_ssqz)
TextView tvWssbjlcxSsqz;
@BindView(R.id.tv_wssbjlcx_sbrq)
TextView tvWssbjlcxSbrq;
@BindView(R.id.tv_wssbjlcx_sbse)
TextView tvWssbjlcxSbse;
public MyViewHolder(View view) {
super(view);
ButterKnife.bind(this, view);
}
}
class FooterViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.tv_wssbjlcx_footer_text)
TextView tvFooter;
public FooterViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
public void setFooterText(String str) {
if (tvFooter != null) {
tvFooter.setText(str);
strFooter = str;
}
}
}
这里就比较偷懒的用一个public方法进行footer item信息的显示,这里也可以使用动画效果等等。
在Activty中的使用与正常RecyclerView的使用没有区别,adapter的绑定也一样。只是需要在解析完数据之后,判断当前获取的数据是否为最后一页的数据,然后对最后一项的item进行设置文字。
最后附上R.layout.item_wssbjlcx_rv_footer
<?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="20dp"
android:orientation="vertical">
<TextView
android:id="@+id/tv_wssbjlcx_footer_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="13sp" />
</LinearLayout>