Android listview數(shù)據(jù)顯示及提示信息的實(shí)例
Android listview數(shù)據(jù)顯示及提示信息的實(shí)例
最近我們測(cè)試人員說,我們的所有的列表都要做一個(gè)沒有數(shù)據(jù)就提示沒有數(shù)據(jù),當(dāng)時(shí)我的表情是這樣的 =_=!!! 我的天吶……這么多列表真的要一個(gè)一個(gè)做嘛?。?!然后我想了一下,那就直接改造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ù)時(shí),顯示的圖片資源--> <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"/>
其實(shí)就是在onDraw里面,檢測(cè)到有幾個(gè)item,如果adapter為null,或者adapter的count小于等于最小item數(shù)的話,就在背景顯示沒有數(shù)據(jù)的圖片出來、
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android編程實(shí)現(xiàn)為L(zhǎng)istView創(chuàng)建上下文菜單(ContextMenu)的方法
- Android UI設(shè)計(jì)系列之自定義ListView仿QQ空間阻尼下拉刷新和漸變菜單欄效果(8)
- Android ListView長(zhǎng)按彈出菜單二種實(shí)現(xiàn)方式示例
- Android實(shí)現(xiàn)讀取SD卡下所有TXT文件名并用listView顯示出來的方法
- Android編程實(shí)現(xiàn)ListView內(nèi)容無(wú)限循環(huán)顯示的方法
- Android TV listview及焦點(diǎn)處理
- Android ListView列表控件的介紹和性能優(yōu)化
- Android中ListView下拉刷新的實(shí)現(xiàn)代碼
- Android ListView分頁(yè)簡(jiǎn)單實(shí)現(xiàn)
- Android ListView 條目多樣式展示實(shí)例詳解
- android開發(fā)教程之listview使用方法
- Android編程實(shí)現(xiàn)帶有圖標(biāo)的ListView并帶有長(zhǎng)按菜單效果示例
相關(guān)文章
Android 如何定制vibrator的各種震動(dòng)模式M 具體方法
這篇文章介紹了Android 如何定制vibrator的各種震動(dòng)模式M 具體方法,有需要的朋友可以參考一下2013-09-09
Activity與Service之間交互并播放歌曲的實(shí)現(xiàn)代碼
以下是對(duì)Activity與Service之間交互并播放歌曲的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下2013-07-07
Android WebView如何判定網(wǎng)頁(yè)加載的錯(cuò)誤
這篇文章主要介紹了Android WebView如何判定網(wǎng)頁(yè)加載的錯(cuò)誤,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-04-04
Android中的Bmob移動(dòng)后端云服務(wù)器功能
這里介紹一個(gè)移動(dòng)后端云服務(wù)器平臺(tái)bmob,這不僅可以實(shí)現(xiàn)云數(shù)據(jù)庫(kù)儲(chǔ)存,還可以獲取手機(jī)驗(yàn)證等,隨時(shí)隨地都很輕松,下面寫一個(gè)小demo,實(shí)現(xiàn)一個(gè)登陸注冊(cè)功能,認(rèn)識(shí)增刪查改2018-01-01
詳解Android使用Gradle統(tǒng)一配置依賴管理
本篇文章主要介紹了詳解Android 使用 Gradle 統(tǒng)一配置依賴管理,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01
Android耳機(jī)插拔檢測(cè)(framework篇)原理解析
文章詳細(xì)介紹了Android系統(tǒng)中音頻設(shè)備插拔事件的處理流程,包括事件的獲取、處理和上報(bào),本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-11-11
Android開發(fā)之基于DialogFragment創(chuàng)建對(duì)話框的方法示例
這篇文章主要介紹了Android開發(fā)之基于DialogFragment創(chuàng)建對(duì)話框的方法,結(jié)合實(shí)例形式分析了DialogFragment創(chuàng)建對(duì)話框的具體功能與布局相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-08-08
android 實(shí)現(xiàn)類似微信緩存和即時(shí)更新好友頭像示例
本篇文章主要介紹了android 實(shí)現(xiàn)類似微信緩存和即時(shí)更新好友頭像示例,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01
Android使用ViewDragHelper實(shí)現(xiàn)QQ6.X最新版本側(cè)滑界面效果實(shí)例代碼
這篇文章主要介紹了Android程序開發(fā)實(shí)現(xiàn)QQ6.X最新版本側(cè)滑界面效果實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-02-02
Android實(shí)現(xiàn)拍照及圖片裁剪(6.0以上權(quán)限處理及7.0以上文件管理)
本篇文章主要介紹了Android實(shí)現(xiàn)拍照及圖片裁剪(6.0以上權(quán)限處理及7.0以上文件管理),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10

