在FrameLayout中为空的ListView显示TextView [英] Show TextView for an empty ListView inside a FrameLayout
问题描述
我有一个选项卡式布局,以及一个使用选项卡作为视图的活动.它具有三个选项卡,如 ListViews
.如果列表中的任何一个为空,我都想显示一个简单的 TextView
.我看过很多文章,但所有文章都讨论了 LinearLayout
中的单个 ListView
.我不确定是否由于多个 ListViews
或 FrameLayout
而无法使用.我们不能在 FrameLayout
内设置 visibility = GONE 吗?因为即使那样, TextView
始终会与 ListView
一起显示.谁能建议在这种情况下可以做什么?
I have a tabbed layout and an activity using tabs as views. It has three tabs as ListViews
. If either of the lists is empty I want to show a simple TextView
instead. I've gone through many posts but all of them talk about a single ListView
inside a LinearLayout
. I'm not sure if it is not working because of multiple ListViews
or FrameLayout
. Can we not set visibility = GONE inside a FrameLayout
? Because even then the TextView
is always shown along with the ListView
. Can anyone suggest what can be done in this scenario?
我还尝试将 TextView
包含在另一个xml文件中.但是我不确定如何将 TextView
添加到我的 FrameLayout
中.
I also tried including TextView
in another xml file. But I'm not sure how to add that TextView
to my FrameLayout
.
这就是我对所有三个 ListViews
TextView empty = (TextView) findViewById(R.id.blank);
FrameLayout frameLayout = (FrameLayout) findViewById(android.R.id.tabcontent);
mListView_top10 = (ListView)findViewById(R.id.Top_10);
if(TopWR.size()!=0) {
mListView_top10.setAdapter(new ArrayAdapter<String>(this, R.layout.listview_row,TopWR));
} else {
frameLayout.addView(empty);
}
FrameLayout
FrameLayout
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/innerdashboard_bg"
android:layout_weight="1">
<ListView
android:id="@+id/Top_10"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingRight="5dp"
android:text="this is a tab" />
<ListView
android:id="@+id/Billable"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingRight="5dp"
android:text="this is another tab" />
<ListView
android:id="@+id/Product"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingRight="5dp"
android:textSize="14sp"
android:text="this is a third tab" />
</FrameLayout>
TextView
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/blank"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No records Avaible"
android:textColor="#ffffff" />
编辑
我也尝试过使用 setEmptyView
,对于使用单独的空白视图的所有三个 ListViews
,都行不通!
I've also tried using setEmptyView
, for all three ListViews
using separate empty views, doesn't work!
TextView empty1 = (TextView) findViewById(R.id.blank1);
mListView_top10 = (ListView)findViewById(R.id.Top_10);
mListView_top10.setEmptyView(empty1);
mListView_top10.setAdapter(new ArrayAdapter<String>(this, R.layout.listview_row,TopWR));
xml:
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/innerdashboard_bg"
android:layout_weight="1">
<LinearLayout
android:id="@+id/Top_10"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/Top_10"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingRight="5dp"
android:text="this is a tab" />
<TextView
android:id="@+id/blank1"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No records Avaible"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:id="@+id/Billable"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/Billable"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingRight="5dp"
android:text="this is another tab" />
<TextView
android:id="@+id/blank2"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No records Avaible"
android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout
android:id="@+id/Product"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/Product"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingRight="5dp"
android:textSize="14sp"
android:text="this is a third tab" />
<TextView
android:id="@+id/blank3"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No records Avaible"
android:textColor="#ffffff" />
</LinearLayout>
</FrameLayout>
推荐答案
Just call setEmptyView(...)
on your ListView
, passing in the TextView
as argument.
TextView empty = (TextView) findViewById(R.id.blank);
mListView_top10.setEmptyView(empty);
ListView
应该自动处理切换空"视图的可见性.
The ListView
should automatically take care of toggling the visibility of the 'empty' view.
提示:由于该参数是通用的 View
,因此您可以传入 View
的任何子类,或更复杂的视图层次结构.
Tip: since the argument is a generic View
, you can pass in any subclass of View
, or more complex view hierarchy.
附带说明:您可能必须为每个 ListView
自己的 TextView
实例提供为空视图,以免发生冲突.例如当一个列表确实包含内容时,而另一个列表则没有.
On a side note: you will probably have to give each ListView
its own TextView
instance as empty view to avoid clashing scenarios; e.g. when one list does have content, while another doesn't.
这篇关于在FrameLayout中为空的ListView显示TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!