首先先描述下我所遇到的问题
1.在adapter里面实现设置空view的时候,list先有数据再清空setnewdata为null的时候就可以显示出来,但是这种情况就没意义了
2.nationAdapter.isUseEmpty(true);添加这个设置依然无效
3.nationAdapter.setEmptyView(LayoutInflater.from(this).inflate(R.layout.empty_build_list, rl_top, false));这一行代码放在setadapter方法前后都无效
经过多重测试修改,最后发现当你的adapter里面重写getItemCount方法的时候,这个setEmptyView方法则无效,需要去掉这个
附语:(网友)
一般我们在初始化adapter时就可以设置好emptyview。如果你要的效果是首次进去不展示emptyview(首次接口没调还没数据)就加个设置useeEmptyview(false),然后在接口返回时,立马在设置useEmptyview(true)。当然,如果首次你要直接展示emptyView,那就直接初始化adapter的地方设置true就可以了,接口失败也要设置true
也感谢群里的网友解惑和协助
接下来放我的代码
activity里面
public void showRcy() {
nationList = new ArrayList<>();
// for (int i = 0; i < nation.length; i++) {
// nationList.add(new NationBean(nation[i]));
// }
NationAdapter nationAdapter = new NationAdapter(nationList, context);
nationAdapter.isUseEmpty(true);
recyclerView.setLayoutManager(new GridLayoutManager(context, 1, GridLayoutManager.VERTICAL, false));
nationAdapter.setEmptyView(LayoutInflater.from(this).inflate(R.layout.empty_build_list, rl_top, false));
recyclerView.setAdapter(nationAdapter);
}
adapter里面
public class NationAdapter extends BaseQuickAdapter<NationBean, BaseViewHolder> {
private List<NationBean> beanList;
private Context context;
public NationAdapter(List<NationBean> beanList, Context context) {
super(R.layout.item_nation, beanList);
this.beanList = beanList;
this.context = context;
}
public void setData(List<NationBean> beanList) {
this.beanList = beanList;
notifyDataSetChanged();
}
public void setSearchData(List<NationBean> beanList) {
this.beanList.clear();
this.beanList.addAll(beanList);
notifyDataSetChanged();
}
@Override
protected void convert(BaseViewHolder helper, NationBean item) {
helper.setText(R.id.tv_content, item.getNationName());
}
// @Override
// public int getItemCount() {
// return beanList.size();
// }
public void setEmpty(){
setNewData(null);
View view = LayoutInflater.from(context).inflate(R.layout.empty_build_list, null);
setEmptyView(view);
}
}