问题
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?
I also tried including TextView in another xml file. But I'm not sure how to add that TextView to my FrameLayout.
This is what I'm doing for m all three 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
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" />
EDIT
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>
回答1:
Just call setEmptyView(...) on your ListView, passing in the TextView as argument.
TextView empty = (TextView) findViewById(R.id.blank);
mListView_top10.setEmptyView(empty);
The ListView should automatically take care of toggling the visibility of the 'empty' view.
Tip: since the argument is a generic View, you can pass in any subclass of View, or more complex view hierarchy.
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.
回答2:
I would insist you to use ViewStub here with a ListView inside a FrameLayout. When your ListView has data you can use VIEW.GONE to ViewStub and if your ListView is no data then use VIEW.VISIBLE for the ViewStub. You can download example from github and get it working.
回答3:
You can solve this by adding an empty item when the list is empty. The following example is based on your pre-EDIT code:
FrameLayout frameLayout = (FrameLayout) findViewById(android.R.id.tabcontent);
mListView_top10 = (ListView)findViewById(R.id.Top_10);
if (TopWR.size() == 0) {
// add a single item to the array, in order to indicate the list is empty
TopWR.add("The List Is Empty");
}
mListView_top10.setAdapter(new ArrayAdapter<String>(this, R.layout.listview_row,TopWR));
If you want to get fancier with how the empty list is displayed you can replace ArrayAdapter with a custom adapter. You can also use a different adapter when the list is empty.
回答4:
Have you tried setting the Visibility on each of the ListViews in your java code?
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.setVisibility(View.VISIBLE);
mListView_top10.setAdapter(new ArrayAdapter<String>(this,
R.layout.listview_row,TopWR));
}
else {
mListView_top10.setVisibility(View.GONE);
empty.setVisibility(View.VISIBLE);
frameLayout.addView(empty);
}
TextView
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/blank"
android:visibility="gone"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No records Avaible"
android:textColor="#ffffff" />
来源:https://stackoverflow.com/questions/11463234/show-textview-for-an-empty-listview-inside-a-framelayout