首先先描述下我所遇到的問題
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方法和getItemViewType的時候,這個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);
}
}