实用ListView加载提示工具类
来源:互联网 发布:淘宝活动倒计时代码 编辑:程序博客网 时间:2024/06/09 18:45
优秀的用户体验中,ListView应该包括这几个状态:
正在加载数据
加载完成但无数据
加载失败
平常使用诸多不便大家应该都有体会,为了开发方便,所以我写了这样一个工具类,不需要修改xml文件,仅需类似下面几行代码即可将上面三种状态呈现出来(在listview所占空间的中间位置显示):
//加载状态时LswLoader loader = new LswLoader(mContentLsw); * loader.loading(R.drawable.ic_launcher, "正在加载...");//加载完成但是没有数据提示loader.loadEnd(R.drawable.ic_launcher, "暂无内容");//加载错误,需要重试loader.loadError(R.drawable.ic_launcher, new View.OnClickListener() { @Override public void onClick(View v) { //重新请求数据 } });
代码见下:
这里的实现思路是将布局添加到listview的父控件中,并设置为listview的emptyview,这样在listview为空时即能显示加载布局。
package com.idroid.widget;import com.idroid.R;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.view.ViewParent;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.ImageView;import android.widget.ListView;import android.widget.TextView;/** * LswLoader loader = new LswLoader(mContentLsw); * loader.loading(R.drawable.ic_launcher, "正在加载..."); *//** * 给listview封装加载样式 * */public class ListViewDecorator { private ImageView mLoadImage; private TextView mLoadText; private ListView mLoadLsw; public ListViewDecorator(ListView listview) { if (listview == null) throw new RuntimeException("需要一个ListView对象"); ViewParent parent = listview.getParent(); if (parent == null) throw new RuntimeException("ListView未加入布局?"); Context context = listview.getContext(); View loadView = View.inflate(context, R.layout.load, null); mLoadImage = (ImageView) loadView.findViewById(R.id.load_image); mLoadText = (TextView) loadView.findViewById(R.id.load_text); if (parent instanceof ViewGroup) { ViewGroup viewGroup = (ViewGroup) parent; int index = viewGroup.indexOfChild(listview); viewGroup.addView(loadView, ++index); } else throw new RuntimeException("使用方式不对"); listview.setEmptyView(loadView); mLoadLsw = listview; } /** * * @param resId 图片id * @param text 加载文字 */ public void loading(int resId, String text) { mLoadImage.setImageResource(resId); mLoadText.setText(text); Animation loadAnim = AnimationUtils.loadAnimation(mLoadLsw.getContext(), R.anim.load_animation); mLoadImage.startAnimation(loadAnim); mLoadImage.setOnClickListener(null); mLoadText.setOnClickListener(null); } /** * 加载为空、加载错误时的提示 * * @param resId 图片id * @param text 加载完成的文字提示 */ public void loadEnd(int resId, String text) { mLoadImage.clearAnimation(); mLoadImage.setImageResource(resId); mLoadText.setText(text); mLoadImage.setOnClickListener(null); mLoadText.setOnClickListener(null); } /** * * @param resId 图片id * @param l 点击重新加载的listener */ public void loadError(int resId, View.OnClickListener l) { mLoadImage.clearAnimation(); mLoadImage.setImageResource(resId); mLoadText.setText("加载失败、点击重试"); mLoadImage.setOnClickListener(l); mLoadText.setOnClickListener(l); }}
布局文件-load.xml
<?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" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/load_image" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/load_text" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout></RelativeLayout>
动画-load_animation.xml
<?xml version="1.0" encoding="utf-8"?><set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android"> <rotate android:interpolator="@android:anim/linear_interpolator" android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0" android:toDegrees="+360" android:duration="1500" android:startOffset="-1" android:repeatMode="restart" android:repeatCount="-1"/> </set>
使用时拷贝三个文件:一个类、一个布局xml、一个动画xml到相应目录即可,赶紧感受一下是否方便了很多罢?
0 0
- 实用ListView加载提示工具类
- 【工具类】Android自定义提示框、对话框、加载框
- 实用Map工具类
- ListView异步加载图片是非常实用的方法
- [简单实用系列] listview下拉刷新,加载更多
- iOS主流进度加载提示工具
- 使用第三方类XListView,实现ListView的加载刷新,提示
- 25个实用的jQuery工具提示插件
- android下listview的优化和listview的异步加载全攻略(非常实用)
- 实用的Http工具类
- 实用的JSON工具类
- 实用的工具类Logutil
- [安卓]手机管家(十四)通讯卫士之工具类及listView的分批加载
- 如何使用Gson工具类,以及使用listview加载相应的解析项
- 如何使用Gson工具类,以及使用listview加载相应的解析项
- 如何使用Gson工具类,以及使用listview加载相应的解析项
- RecyclerView 的工具类,更方便的实现 Adapter,item 点击事件,更快的实现加载提示,分页加载。
- 加载图片工具类
- HDU 1.3.1 FatMouse' Trade
- Android之来电秀实现(一)
- Rust hello world 语法讲解
- HDU 1.3.2 Tian Ji -- The Horse Racing
- 61-题目1076:N的阶乘
- 实用ListView加载提示工具类
- HDU 1.3.3 开门人和关门人
- iOS电池条颜色修改
- 自定义ImageLoader实现GridView加载图片
- 第2周项目3—小试循环(4)
- HDU 1.3.4 What Is Your Grade?
- nginx http 与https 共存,并设置参数
- appium上调试ios真机的各种问题
- iOS通过UIApplication获取网络状态