Android 自定义空数据提示界面 EmptyView

关于Fastandrutils

Fastandrutils 是一套整理修改整合的android开发常用的工具类,常用的自定义view控件等。
这样可以减少复制粘贴代码,从而减少重复代码,也不用为了一个常用的功能去谷歌百度,让代码更简洁,让开发更高效。
同时希望您的添加完善,让android开发变得更简单。

github地址,感兴趣的话,不妨点赞支持下
个人博客


打完广告,进入正题
不多说,先上图

在开发过程中,最常的就是数据和界面间的交互,例如无数据时的界面展示,网络不通时的界面展示,对于这些不是正常的数据,我们都要做一些异常的展示界面,而不是一个空白界面,这样做一些异常界面的处理,用户体验上会更好点。
源码地址

功能

  1. 显示加载界面
  2. 显示失败界面
  3. 可自定义空界面

FEmptyView说明

代码有点多,就贴些关键的
- FEmptyView 继承 FrameLayout
- FEmptyView里的属性

    private LinearLayout setdataLay;//设置数据布局
    private View emptyView;//空布局
    private ImageView emptyImg;//空布局的ImageView
    private TextView emptyTv;//空布局的TextView
    private Button emptyBt;//空布局的Button
    private Context context;
  • 为了自定义界面可配置,添加自定义属性attrs
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="EmptyLayout">
        <attr name="empty_layout" format="reference" />//空界面的layout文件
        <attr name="empty_imageView" format="reference" />//空界面imageView的id
        <attr name="empty_textView" format="reference" />//空界面textView的id
        <attr name="empty_button" format="reference" />//空界面button的id
        <attr name="include_layout" format="reference" />//数据展示界面的layout文件
    </declare-styleable>
</resources>
  • FEmptyView 初始化界面布局
 private void initView(TypedArray array) {
        int emptyViewId = array.getResourceId(FResourcesUtils.getStyleable("EmptyLayout_empty_layout"), FResourcesUtils.getLayoutResources("f_empty_layout"));
        int emptyImgId = array.getResourceId(FResourcesUtils.getStyleable("EmptyLayout_empty_imageView"), FResourcesUtils.getIdResources("empty_img"));
        int emptyTvId = array.getResourceId(FResourcesUtils.getStyleable("EmptyLayout_empty_textView"), FResourcesUtils.getIdResources("empty_tv"));
        int emptyBtId = array.getResourceId(FResourcesUtils.getStyleable("EmptyLayout_empty_button"), FResourcesUtils.getIdResources("empty_bt"));
        int dataViewId = array.getResourceId(FResourcesUtils.getStyleable("EmptyLayout_include_layout"), 0);
        //获取空布局View
        emptyView = View.inflate(context, emptyViewId, null);
        //获取空布局的imageView
        emptyImg = (ImageView) emptyView.findViewById(emptyImgId);
        emptyTv = (TextView) emptyView.findViewById(emptyTvId);
        emptyBt = (Button) emptyView.findViewById(emptyBtId);
        setdataLay = new LinearLayout(context);//先添加一个LinearLayout
        setdataLay.setOrientation(LinearLayout.VERTICAL);
        setdataLay.setVisibility(GONE);
        if (dataViewId != 0) {
            addChildViewid(dataViewId);//把数据界面添加到LinearLayout里
        }
        addView(setdataLay);
        addView(emptyView);
    }
  • 默认空界面布局 有一个ImageView,一个TextView,Button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/empty_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxHeight="200dp"
        android:maxWidth="200dp" />

    <TextView
        android:id="@+id/empty_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:textSize="16sp" />

    <Button
        android:id="@+id/empty_bt"
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:layout_marginTop="6dp" />

</LinearLayout>
  • FEmptyView 的一些方法
    void loadding()//显示加载界面
    void loaddingSuccess() 成功获取到数据把EmptyView隐藏
    void loaddingFail() 加载失败界面

    省略了比较多方法,详细看源码

  • ImagView 回调
    为了更好的展示ImagView 就写了个回调方法,自行解决ImagView的动画等

/**
 * ImagView 回调
 */
    public interface ImgCallBack {
        void setImg(ImageView img, int emptyType);
    }
  • emptyView 回调
    如果要做更深度自定义emptyView,这里也返回了emptyView,这样就可以获取所有emptyView的控件
/**
     * emptyView 回调
     */
    public interface ViewCallBack {
        void emptyViewCallBack(View view);
    }

FEmptyView使用

第一种使用方法

  1. 在需要使用的xml添加
 <cn.hotapk.fastandrutils.widget.FEmptyView
        android:id="@+id/empty_lay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:include_layout="@+layout/data_layout" />

FEmptyView 不能在xml下加子View
只能这样添加数据界面

  app:include_layout="@+layout/data_layout"

在oncreat() 方法中

 empty_lay = (FEmptyView) findViewById(R.id.empty_lay);
 empty_lay.loadding("正在加载数据。。。");
 empty_lay.loaddingFail("加载数据失败。。。", "点击刷新", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        loadding();
                    }
                }, new FEmptyView.ImgCallBack() {
                    @Override
                    public void setImg(ImageView img, int emptyType) {
                        img.setBackgroundResource(R.mipmap.loaddingfail);
                        if (img.getAnimation() != null)
                            img.getAnimation().cancel();
                    }
                });

第二种使用方法

<cn.hotapk.fastandrutils.widget.FEmptyView
        android:id="@+id/empty_lay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

FEmptyView 不能在xml下加子View
只能在oncreat() 中这样添加数据界面

        empty_lay.addChildView(childView);

之后的使用第一种

第三种使用方法

可自定义emptyView

 <cn.hotapk.fastandrutils.widget.FEmptyView
        android:id="@+id/empty_lay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:empty_button="@+id/custom_empty_bt"//自定义layout的button的id
        app:empty_imageView="@+id/custom_empty_img"//自定义layout的imageView的id
        app:empty_textView="@+id/custom_empty_tv"//自定义layout的textView的id
        app:empty_layout="@+layout/custom_empty_layout"//自定义的空 layout界面
        app:include_layout="@+layout/data_layout" />//展示数据的layout

FEmptyView 同样不能在xml下加子View
只能这样添加数据界面

  app:include_layout="@+layout/data_layout"

之后的使用同第一种

第四种使用方法

可深度自定义emptyView

 <cn.hotapk.fastandrutils.widget.FEmptyView
        android:id="@+id/empty_lay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:empty_layout="@+layout/custom_empty_layout"//自定义的空 layout界面
        app:include_layout="@+layout/data_layout" />//展示数据的layout

深度自定义的话
代码中会返回自定义的emptyView

    public View getEmptyView() {
        return emptyView;
    }

可以findViewById获取自定义的控件
之后的使用同第一种差不多

Android 自定义空数据提示界面 EmptyView 解说完毕

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值