[Android]ListView的setEmptyView的替代方法

当ListView内容为空时,我们显示一个“内容为空”的提示,用setEmptyView,如下:
<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>

其实,我开始使用的这个方法,后来由于扩展性差,就还是用了推荐的方法。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值