ListView/ExpandableListView setEmptyView()无效

87e*_*ent 5 android listview expandablelistview

我正在尝试为三个列表视图设置空视图,即2个可扩展视图和一个列表视图,它们位于不同的选项卡中:

LayoutInflater inflater = getLayoutInflater();
        View emptyView = inflater.inflate(R.layout.empty_view, null, false);
...
ExpListView1.setEmptyView(emptyView);
ListView.setEmptyView(emptyView);
ExpListView2.setEmptyView(emptyView);
Run Code Online (Sandbox Code Playgroud)

它没有任何效果 - 不显示空视图.与该代码段相同:

View emptyView = inflater.inflate(R.layout.empty_view, null, false);
View emptyRelativeLayout = (RelativeLayout) emptyView.findViewById(R.id.empty_view_layout);
ExpListView1.setEmptyView(emptyRelativeLayout);
Run Code Online (Sandbox Code Playgroud)

empty_view_layout.xml:

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:id="@+id/empty_view_layout">
    <TextView
    style="@android:style/TextAppearance.Large"
    android:id="@+id/empty_view_textview"
    android:layout_width = "fill_parent"
    android:layout_height="fill_parent"
    android:text="@string/no_items_in_list" 
    android:layout_centerInParent="true"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

87e*_*ent 6

通过将空视图添加为我的Activity的另一个内容视图来解决问题:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    inflater = getLayoutInflater();
    emptyView = inflater.inflate(R.layout.empty_view, null);
    addContentView(emptyView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
Run Code Online (Sandbox Code Playgroud)

  • 我只是理解了这一切是如何工作的,我想要分享:列表(`android.R.id.list`)和空视图(`android.R.id.empty`)都必须附加到活动中contentView,这也可以通过将它们放在`setContentView`使用的布局xml文件中来实现,所以没有通货膨胀.如果他们有上述ID,也不需要调用`setEmptyView`:参见http://stackoverflow.com/a/2767974/253468.将它们放在同一个文件中的优点是,您可以自定义它显示的位置以及如何最简单地将它们包装在`FrameLayout`中. (2认同)