ViewGroup为空时显示空视图

时间:2016-07-22 17:22:34

标签: java android viewgroup

Showing empty view when ListView is empty类似,但我希望在没有子项时在我的ViewGroup(LinearLayout,RelativeLayout等)中显示文本。我尝试过setOnHierarchyChangeListener,但它会导致NPE。我相信它可能与我正在设置布局变化的事实有关 - 我之前有过与之相关的错误,但我想知道是否有更好的方法。这是我目前的代码。

flowLayout.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
    LayoutInflater inflater = getLayoutInflater();
    RelativeLayout textView = (RelativeLayout) inflater.inflate(R.layout.widget_no_widgets, flowLayout, false);

    @Override
    public void onChildViewAdded(View parent, View child) {
        if (child != textView) {
            if (textView.getParent() == flowLayout) {
                flowLayout.removeView(textView);
            }
        }
    }

    @Override
    public void onChildViewRemoved(View parent, View child) {
        if (flowLayout.getChildCount() == 0) {
            flowLayout.addView(textView);
        }
    }
});

0 个答案:

没有答案
相关问题