Android listview數(shù)據(jù)顯示及提示信息的實例
Android listview數(shù)據(jù)顯示及提示信息的實例
最近我們測試人員說,我們的所有的列表都要做一個沒有數(shù)據(jù)就提示沒有數(shù)據(jù),當(dāng)時我的表情是這樣的 =_=!!! 我的天吶……這么多列表真的要一個一個做嘛?。?!然后我想了一下,那就直接改造Listview吧……說干就干。
沒有數(shù)據(jù)的效果:

有數(shù)據(jù)的效果:

代碼: NoDataListView.java
package com.tianjs.tianjinsuop2p.widgets;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.Adapter;
import android.widget.ListView;
import com.tianjs.tianjinsuop2p.R;
/**
* Created by xiaolei on 2017/4/28.
*/
public class NoDataListView extends ListView
{
private int height = 0;
private int width = 0;
private Bitmap noDataBitmap = null;
private Paint mPaint;
private int minItem = 0;
private int noDataImgInt = 0;
public NoDataListView(Context context)
{
this(context, null);
}
public NoDataListView(Context context, AttributeSet attrs)
{
this(context, attrs, 0);
}
public NoDataListView(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.NoDataListView);
noDataImgInt = array.getResourceId(R.styleable.NoDataListView_noDataImg, 0);
minItem = array.getInt(R.styleable.NoDataListView_minItem, 0);
if (noDataImgInt != 0)
{
noDataBitmap = BitmapFactory.decodeResource(getResources(), noDataImgInt);
} else
{
noDataBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
}
mPaint = new Paint();
array.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
height = MeasureSpec.getSize(heightMeasureSpec);
width = MeasureSpec.getSize(widthMeasureSpec);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas)
{
Adapter adapter = getAdapter();
super.onDraw(canvas);
if (adapter == null || adapter.getCount() <= minItem)
{
canvas.drawBitmap(noDataBitmap, width / 2 - noDataBitmap.getWidth() / 2, height / 2 - noDataBitmap.getHeight() / 2, mPaint);
}
}
}
屬性聲明:
<declare-styleable name="NoDataListView"> <!--最低Item數(shù)--> <attr name="minItem" format="integer"/> <!--沒有數(shù)據(jù)時,顯示的圖片資源--> <attr name="noDataImg" format="reference"/> </declare-styleable>
如何使用?
<com.tianjs.tianjinsuop2p.widgets.NoDataListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#00000000"
android:dividerHeight="0dp"
android:overScrollMode="never"
android:scrollbars="none"
app:minItem="0"
app:noDataImg="@drawable/icon_wushuju"/>
其實就是在onDraw里面,檢測到有幾個item,如果adapter為null,或者adapter的count小于等于最小item數(shù)的話,就在背景顯示沒有數(shù)據(jù)的圖片出來、
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- Android編程實現(xiàn)為ListView創(chuàng)建上下文菜單(ContextMenu)的方法
- Android UI設(shè)計系列之自定義ListView仿QQ空間阻尼下拉刷新和漸變菜單欄效果(8)
- Android ListView長按彈出菜單二種實現(xiàn)方式示例
- Android實現(xiàn)讀取SD卡下所有TXT文件名并用listView顯示出來的方法
- Android編程實現(xiàn)ListView內(nèi)容無限循環(huán)顯示的方法
- Android TV listview及焦點處理
- Android ListView列表控件的介紹和性能優(yōu)化
- Android中ListView下拉刷新的實現(xiàn)代碼
- Android ListView分頁簡單實現(xiàn)
- Android ListView 條目多樣式展示實例詳解
- android開發(fā)教程之listview使用方法
- Android編程實現(xiàn)帶有圖標(biāo)的ListView并帶有長按菜單效果示例
相關(guān)文章
Android從源碼的角度徹底理解事件分發(fā)機(jī)制的解析(上)
這篇文章主要介紹了Android從源碼的角度徹底理解事件分發(fā)機(jī)制的解析,具有很好的參考價值,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
Android調(diào)用OpenCV2.4.10實現(xiàn)二維碼區(qū)域定位
這篇文章主要為大家詳細(xì)介紹了Android調(diào)用OpenCV 2.4.10實現(xiàn)二維碼區(qū)域定位,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03
Android N 7.0中報錯:android.os.FileUriExposedException的解決方法
這篇文章主要給大家介紹了關(guān)于在Android N 7.0中報錯:android.os.FileUriExposedException的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧2018-05-05
Android三種網(wǎng)絡(luò)通訊方式及Android的網(wǎng)絡(luò)通訊機(jī)制
在android平臺目前提供了三種網(wǎng)絡(luò)接口可以使用:分別是java.net.*(標(biāo)準(zhǔn)Java接口)、Org.apache接口和Android.net.*(Android網(wǎng)絡(luò)接口),本文主要給大家介紹android三種網(wǎng)絡(luò)通訊方式及android的網(wǎng)絡(luò)通訊機(jī)制,小伙伴們一起學(xué)習(xí)吧2015-11-11
Android使用OkHttp請求自簽名的https網(wǎng)站的示例
本篇文章主要介紹了Android使用OkHttp請求自簽名的https網(wǎng)站的示例,非常具有實用價值,需要的朋友可以參考下、2017-09-09

