实用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