ListView / ExpandableListView setEmptyView() 没有效果

发布于 2024-12-04 05:27:25 字数 1180 浏览 5 评论 0原文

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

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

它没有效果 - 不会出现空视图。与该代码段相同:

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

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>

I'm trying to set empty view for three list views, namely 2 expandable and one list view, which are in different tabs:

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

It does no effect - empty view doesn't appear. Same with that code piece:

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

林空鹿饮溪 2024-12-11 05:27:25

通过将空视图作为另一个内容视图添加到我的活动中解决了问题:

@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));

The problem was solved by adding empty view as another content view to my 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));
明媚殇 2024-12-11 05:27:25

我这样做了:

activity_main.xml

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ExpandableListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/expListView">

        </ExpandableListView>


        <TextView
            android:id="@+id/txtEmptyView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:padding="10dp"
            android:text="@string/no_game_available"
            android:textAppearance="@android:style/TextAppearance.Medium"
            android:textColor="@android:color/white" />

</RelativeLayout>

MainActivity.java

ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);

TextView txtEmptyView = (TextView) view.findViewById(R.id.txtEmptyView);

expandableListView.setEmptyView(txtEmptyView);

希望这会对您有所帮助。

I have done this way:

activity_main.xml

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ExpandableListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/expListView">

        </ExpandableListView>


        <TextView
            android:id="@+id/txtEmptyView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:padding="10dp"
            android:text="@string/no_game_available"
            android:textAppearance="@android:style/TextAppearance.Medium"
            android:textColor="@android:color/white" />

</RelativeLayout>

MainActivity.java

ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);

TextView txtEmptyView = (TextView) view.findViewById(R.id.txtEmptyView);

expandableListView.setEmptyView(txtEmptyView);

Hope this will help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文