01. RecyclerView添加分割线
使用RecyclerView.addItemDecoration()方法
参考:RecyclerView调用addItemDecoration添加自定义分割线
参考:RecyclerView ItemDecoration 底部分割线
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
// 添加分割线
recyclerView.addItemDecoration(new DividerItemDecoration(MAinActivity.this, DividerItemDecoration.VERTICAL));
02. RecyclerView数据更新的 notifyDataSetChanged()方法无效
当数据List更新之后,使用notifyDataSetChanged()方法条目未被刷新。
参考:Android notifyDataSetChanged()方法 无效错误及踩坑 #https://blog.csdn.net/gufeichn/article/details/54409171
在数据更新的时候不能使用:
private MyRecyclerAdapter mRecyclerAdapter; // RecyclerView的适配器Adapter
private List<DataBean> mDataBeanList; // RecyclerView的数据列表
...
// 数据更新的方法
void updateData(List<DataBean> dataBeanList){
mDataBeanList = dataBeanList; // 这种直接赋值的方式是错误的
mRecyclerAdapter.notifyDataSetChanged();
}
而应该使用如下方式:
private MyRecyclerAdapter mRecyclerAdapter; // RecyclerView的适配器Adapter
private List<DataBean> mDataBeanList = new ArrayList<>(); // RecyclerView的数据列表
...
// 数据更新的方法
void notifyData(List<DataBean> dataBeanList){
mDataBeanList.clear(); // 清空数据列表
mDataBeanList.addAll(dataBeanList); // 添加新数据
mRecyclerAdapter.notifyDataSetChanged(); // 通知适配器更新数据源
}