代码如下:
?xml version="1.0" encoding="utf-8"?
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/
ImageView android:id="@+id/ivEmpt"
android:src="@drawable/netstate_icon"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/
ViewStub
android:id="@+id/vsLoadView"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="@layout/loadding_view"
/
/RelativeLayout
java文件
代码如下:
package com.bvin.test.view;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewStub;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private ListView lv = null;
private View vEmpt = null;
private ArrayAdapterString adapter = null;
private ListString sta = new ArrayListString();
private ViewStub vsLoadView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
lv = (ListView)findViewById(R.id.lv);
adapter = new ArrayAdapterString(this,android.R.layout.simple_list_item_1,sta);
lv.setAdapter(adapter);
vEmpt=findViewById(R.id.ivEmpt);
lv.setEmptyView(vEmpt);
vsLoadView = (ViewStub)findViewById(R.id.vsLoadView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, 0, 0, "添加");
menu.add(1, 1, 1, "删除");
menu.add(2, 2, 2, "清空");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case 0:
sta.add("萨芬i阿双方i");
if(!vsLoadView.isShown())
vsLoadView.inflate();
break;
case 1:
if(sta.size()0)
sta.remove(sta.size()-1);
vsLoadView.setVisibility(View.GONE);
break;
case 2:
sta.clear();
vsLoadView.setVisibility(View.VISIBLE);
break;
}
adapter.notifyDataSetChanged();
return super.onOptionsItemSelected(item);
}
}
前面那个加载的logo,后面的是内容为空的时候显示的Logo。
得出的结论是:
1.ViewStub的inflate()方法只能调用一次,调用了inflate()调用setVisibility(View.VISIBLE);设置为可见,但是调用了setVisibility(View.VISIBLE);就不能再调用inflate(),因为这个方法setVisibility是先把布局inflate出来,然后再设置可见。
2. listView.setEmptyView();这个方法也有点讲究,就是EmptyView与Listview在同一个容器里,负责无效。