当ListView内容为空时,我们显示一个“内容为空”的提示,用setEmptyView,如下:
在代码里面使用如下调用:
以上的是最通用的实现,我原来的实现是继承ListView,进行重绘,如下:
在XML中需要给ListView指定Tag,我们把需要显示的字符串放在Tag里,在绘制时取出来:
其实,我开始使用的这个方法,后来由于扩展性差,就还是用了推荐的方法。
<LinearLayout android:id="@+id/content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FF000000">
......
<ListView android:id="@+id/list_result"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fastScrollEnabled="true" />
<TextView android:id="@+id/list_empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="@string/TAG_NO_RESULT_CONTENT"
android:textSize="16sp"
android:textColor="#FF808080"
android:padding="5dp"
android:visibility="gone" />
</LinearLayout>
在代码里面使用如下调用:
list = (ListView)findViewById( R.id.list_result );
list.setEmptyView( findViewById( R.id.list_empty ) );
以上的是最通用的实现,我原来的实现是继承ListView,进行重绘,如下:
package com.xxxx.yyyy;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.content.Context;
import android.graphics.Canvas;
import android.widget.ListView;
public class EmptyListView extends ListView
{
private final TextPaint paint;
public EmptyListView( Context context ){ this( context, null ); }
public EmptyListView( Context context, AttributeSet attrs ){ this( context, attrs, android.R.attr.listViewStyle ); }
public EmptyListView( Context context, AttributeSet attrs, int defStyle )
{
super( context, attrs, defStyle );
this.paint = new TextPaint();
this.paint.setColor( Color.GRAY );
this.paint.setTextSize( getFontSize( context, 16 ) );
this.paint.setAntiAlias( true );
this.paint.setTextAlign( Align.CENTER );
}
@Override public void onMeasure( int widthMeasureSpec, int heightMeasureSpec )
{
super.onMeasure( widthMeasureSpec, heightMeasureSpec );
}
@Override public void onDraw( Canvas canvas )
{
super.onDraw( canvas );
if( canvas == null || getCount() > 0 ){ return; }
Object empty = super.getTag(); if( empty == null ){ return; }
int offsetX = getWidth() / 2, offsetY = getHeight() / 2;
canvas.translate( offsetX, offsetY );
new StaticLayout( string, paint, getWidth() - 10/*padding*/, Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true ).draw( canvas );
canvas.translate( - offsetX, - offsetY );
}
private float getFontSize( Context context, float value )
{
if( context == null ){ return value; }
Resources resources = context.getResources();
if( resources == null ){ return value; }
DisplayMetrics metrics = resources.getDisplayMetrics();
if( metrics == null ){ return value; }
return value * metrics.density;
}
}
在XML中需要给ListView指定Tag,我们把需要显示的字符串放在Tag里,在绘制时取出来:
<LinearLayout android:id="@+id/content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FF000000">
......
<com.xxxx.yyyy.EmptyListView android:id="@+id/list_result"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:tag="This is an example"
android:fastScrollEnabled="true" />
</LinearLayout>
其实,我开始使用的这个方法,后来由于扩展性差,就还是用了推荐的方法。