/**
* 设置ListView的EmptyView
* setEmptyView
*
* @param listview
* @param emptyView <p>将EmptyView添加到最外层的ViewGroup上。</p>
* @author Administrator
*/
public static void setEmptyView(ListView listview, View emptyView) {
FrameLayout emptyLayout;
if (listview.getEmptyView() == null) {
emptyLayout = new FrameLayout(listview.getContext());
emptyLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
emptyLayout.addView(emptyView);
emptyView.setVisibility(View.VISIBLE);
getParentView((ViewGroup) listview.getParent()).addView(emptyLayout);
listview.setEmptyView(emptyLayout);
} else {
emptyLayout = (FrameLayout) listview.getEmptyView();
emptyLayout.removeAllViews();
emptyLayout.setVisibility(View.VISIBLE);
emptyLayout.addView(emptyView);
}
}
private static ViewGroup getParentView(ViewGroup parent) {
ViewGroup tempVg = parent;
if (parent.getParent() != null && parent.getParent() instanceof ViewGroup) {
tempVg = (ViewGroup) parent.getParent();
getParentView(tempVg);
} else {
return tempVg;
}
return tempVg;
}
来源:CSDN
作者:来瓶94年的爽歪歪
链接:https://blog.csdn.net/gmc940103/article/details/103455516