在Recyclerview末尾添加一个item
-
在网上找了半天没有找到想要的效果,所以自己想了一个比较简单的方法。先上效果图(请忽略那丑丑的界面)
这个是在书架有书的情况下即list.size()>0
这个是list.size()=0时显示的,将原本为“gone”的LinearLayout改成可见的。 -
BookAdapter中的onBindViewHolder代码
@Override
public void onBindViewHolder(ViewHolder holder, int position){
if(position>mbooklist.size()){
holder.bImage.setImageResource(R.drawable.back);
holder.bname.setText("");
holder.bRead.setText("");
holder.bState.setText("");
}else{
book book = mbooklist.get(position);
holder.bImage.setImageResource(R.drawable.book1);
holder.bState.setText(book.getState());
holder.bRead.setText(book.getRead());
holder.bname.setText(book.getEname());
}
if(position==mbooklist.size()-1){
onBindViewHolder(holder,mbooklist.size()+1);
}
}
- MainActivity的部分代码:
if(books.size()==0){
LinearLayout L=(LinearLayout) findViewById(R.id.gone_l);
L.setVisibility(view.VISIBLE);
}else{
RecyclerView recyclerView=(RecyclerView) findViewById(R.id.recycler_view);
GridLayoutManager layoutManager=new GridLayoutManager(this,3);
recyclerView.setLayoutManager(layoutManager);
BookAdapter adapter=new BookAdapter(books);
recyclerView.setAdapter(adapter);
}
参考文章:
简书的文章——RecyclerView
来源:CSDN
作者:qq_43379954
链接:https://blog.csdn.net/qq_43379954/article/details/103905831