今天做项目的时候,准备优化下ListView相关的东西,于是乎,需要做一个当列表无数据时,空的提醒页面。这个自然想到的是ListView的setEmptyView()方法,于是顺手就写了,可是,当我为一个含有HeaderView的ListView设置setEmptyView时,发现:headerView不见了。
未设置EmptyView:
ArrayList teams = new ArrayList<Team>(); DistributeTeamAdapter teamAdapter = new DistributeTeamAdapter(this , teams); View headerView = LayoutInflater.from(getActivity()).inflate(R.layout.header_view_team_list, null); actualListView.addHeaderView(headerView); ViewUtils.inject(this, headerView); lvTeams.setAdapter(teamAdapter);效果图:
加了EmptyView:
ArrayList teams = new ArrayList<Team>(); DistributeTeamAdapter teamAdapter = new DistributeTeamAdapter(this , teams); View headerView = LayoutInflater.from(getActivity()).inflate(R.layout.header_view_team_list, null); actualListView.addHeaderView(headerView); ViewUtils.inject(this, headerView); View layoutEmptyView = LayoutInflater.from(getActivity()).inflate(R.layout.empty_view_of_list, null); lvTeams.setEmptyView(layoutEmptyView); lvTeams.setAdapter(teamAdapter);
效果图:
如代码及示意图,不能同时为listview设置headerView和emptyView!
如有错误,欢迎指正!
如果解决方案,更欢迎指教!