打造万能的EmptyView

简介: 在加载网络数据的时候,我们要给用户一个友好的加载数据的界面,提示正在加载、加载失败和加载成功的功能,其实原理很简单,只是在显示View的上面盖了一层自定义的View,覆盖View的话当然是用RelativeLayout啦,其实思想是在AndroidChina里面看见的,打造不一样的EmptyView,现在再次记录一下,做个笔记。效果图 还是直接看主代码吧Em

在加载网络数据的时候,我们要给用户一个友好的加载数据的界面,提示正在加载、加载失败和加载成功的功能,其实原理很简单,只是在显示View的上面盖了一层自定义的View,覆盖View的话当然是用RelativeLayout啦,其实思想是在AndroidChina里面看见的,打造不一样的EmptyView,现在再次记录一下,做个笔记。

  • 效果图
    这里写图片描述

还是直接看主代码吧

  • EmptyViewLayout.java

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

@SuppressLint("NewApi")
public class EmptyViewLayout extends RelativeLayout {
    private ImageView failure;//加载失败的图片
    private ProgressBar progressBar;//正在loading的pb
    private View bindView;// 绑定的View,即要显示的View

    public EmptyViewLayout(Context context) {
        super(context);
        initView(context);
    }

    public EmptyViewLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.empty_view,
                null);
        failure = (ImageView) view.findViewById(R.id.loading_failure);
        progressBar = (ProgressBar) view.findViewById(R.id.loading_ing);
        /*
         * 这一步添加param必须得要,很多人会遇到inflater添加进来的布局
         * 显示的时候只能显示包容的布局,根本不能match整个布局,然后又说,
         * 我布局文件是match的啊,然后一直找不到问题的解决方法,
         * 我感觉问题应该是出在自定义布局add这个xml的时候需要重新设置宽高吧
         * 所以,我们在自定义控件需要添加xml布局的时候,记得add这个布局的时候
         * 也设置一下param,问题就会解决了
         */
        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                new LayoutParams(LayoutParams.MATCH_PARENT,
                        LayoutParams.MATCH_PARENT));
        addView(view, param);
    }

    /**
     * 总共有三种状态显示:
     * 一种是正在加载状态,
     * 一种是加载失败,
     * 一种是加载成功
     */
    public void Loading() {
        setVisibility(View.VISIBLE);
        if (bindView != null) {
            progressBar.setVisibility(View.VISIBLE);
            failure.setVisibility(View.GONE);
        }
    }

    public void succees() {
        setVisibility(View.GONE);
        if (bindView != null) {
            bindView.setVisibility(View.VISIBLE);
        }
    }

    public void failure() {
        setVisibility(View.VISIBLE);
        if (bindView != null) {
            failure.setVisibility(View.VISIBLE);
            progressBar.setVisibility(View.GONE);
        }
    }

    // 绑定加载 的view
    public void bindView(View view) {
        this.bindView = view;
    }

    /*
     * 利用反射机制,响应对方需要响应的方法
     */
    public void buttonClick(final Object base, final String method) {
        failure.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Method m = base.getClass().getDeclaredMethod(method);
                    m.setAccessible(true);
                    m.invoke(base, null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }
}
  • empty_view.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" >

    <ImageView
        android:id="@+id/loading_failure"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/failure"
        android:visibility="gone" />

    <ProgressBar
        android:id="@+id/loading_ing"
        android:layout_width="130dp"
        android:layout_height="200dp"
        android:layout_centerInParent="true"
        android:indeterminateDrawable="@drawable/empty_progressbar" />

</RelativeLayout>
  • ProgressBar的布局动画empty_progressbar.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:drawable="@drawable/bg_default_layout_loading1"
        android:duration="150">
    </item>
    <item
        android:drawable="@drawable/bg_default_layout_loading2"
        android:duration="150">
    </item>

</animation-list>
  • activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.emptyview.MainActivity" >

    <com.example.emptyview.EmptyViewLayout
        android:id="@+id/emptyView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </com.example.emptyview.EmptyViewLayout>

    <LinearLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff0000"
        android:orientation="vertical"
        android:visibility="gone" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="加载成功"
            android:textColor="#fff"
            android:textSize="30dp" />
    </LinearLayout>

</RelativeLayout>
  • MainActivity.java
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
    private EmptyViewLayout empty;
    private LinearLayout linear;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        empty = (EmptyViewLayout) findViewById(R.id.emptyView);
        linear = (LinearLayout) findViewById(R.id.content);
        empty.bindView(linear);// 绑定要显示的View
        // 加载失败后点击的时候执行onload方法(比如onload里面放的是加载网络数据啊)
        empty.buttonClick(this, "onload");
        onload();
    }

    public void onload() {
        empty.Loading();//empty布局显示正在加载
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //用随机数来判定
                Random r = new Random();
                int res = r.nextInt(3);
                if (res == 0) {
                    empty.succees();//加载成功
                } else {
                    empty.failure();//加载失败
                }
            }
        }, 2000);
    }

}

感觉还行吧,算是给自己做个笔记了,上面那个链接可能说的更明白一点,希望能帮助到大家

目录
相关文章
|
4月前
|
分布式计算 监控 大数据
大数据之路:阿里巴巴大数据实践——离线数据开发
该平台提供一站式大数据开发与治理服务,涵盖数据存储计算、任务调度、质量监控及安全管控。基于MaxCompute实现海量数据处理,结合D2与DataWorks进行任务开发与运维,通过SQLSCAN与DQC保障代码质量与数据准确性。任务调度系统支持定时、周期、手动运行等多种模式,确保高效稳定的数据生产流程。
大数据之路:阿里巴巴大数据实践——离线数据开发
|
SQL 分布式计算 Java
数据治理之元数据管理的利器——Atlas入门宝典(二)
随着数字化转型的工作推进,数据治理的工作已经被越来越多的公司提上了日程。作为Hadoop生态最紧密的元数据管理与发现工具,Atlas在其中扮演着重要的位置。但是其官方文档不是很丰富,也不够详细。所以整理了这份文档供大家学习使用。
3350 1
数据治理之元数据管理的利器——Atlas入门宝典(二)
抖音直播间刷屏打字发言脚本,全自动弹幕插件发广告插件,按键精灵智能防风控版
这是一款用于直播间批量发送弹幕信息的插件源码,可实现自动刷屏、虚拟欢迎与持续点赞功能。通过预设广告文字和随机话术,模拟真实用户行为以规避风控
|
7月前
|
小程序 UED
拓展校友网络的创新解决方案:校园论坛圈子小程序+跑腿+二手市场系统
这是一款基于小程序的校园跑腿服务平台,支持多种注册登录方式、下单支付、跑腿接单配送、订单跟踪评价及物流查询功能,并配备客服模块提升用户体验。系统包含用户、客服、物流、跑腿员和订单五大核心模块,功能完善。此外,平台还拓展了校友网络功能,如信息咨询发布、校园社区建设和活动组织等,旨在增强校友互动与联系,形成紧密的校友生态。
251 4
|
9月前
|
前端开发 算法 Java
《通义灵码2.0体验感受》
《通义灵码2.0体验感受》
|
10月前
|
存储 人工智能 芯片
《光存储与3D存储:开启人工智能硬件存储新时代》
在人工智能快速发展的背景下,数据存储技术的重要性日益凸显。光存储(如全息、多维、超分辨光存储)和3D存储(如3D NAND闪存、3D NVM)等新型技术,以其高密度、高速度的优势,为AI硬件带来全新机遇。这些技术不仅能大幅提升数据处理效率,支持实时决策,还面临成本、稳定性和兼容性等挑战。未来,科研人员和企业需共同努力,推动这些技术与AI硬件的深度融合,助力AI创新与发展。
263 13
|
11月前
|
SEO
为什么大家都推荐PageAdmin CMS?
PageAdmin CMS是一款功能强大、易于使用、免费开源的网站管理系统,适用于政务、企业、学校等门户网站,具备丰富的功能插件和SEO功能,是建站者的理想选择。
322 5
|
机器学习/深度学习 人工智能 自然语言处理
【2024泰迪杯】C 题:竞赛论文的辅助自动评阅 26页及31页2篇完整论文及Python 代码实现
【8月更文挑战第9天】本文介绍了2024年泰迪杯C题的解决方案,该题目旨在构建一个基于AI的学术论文自动评审模型,通过使用开源大语言模型和自然语言处理技术,自动化地评阅竞赛论文,并根据论文的完整性、实质性工作、摘要质量和写作水平进行打分,最终输出符合特定分布的综合评分结果。
332 6
|
安全 Unix Linux
Linux的优点和缺点
【8月更文挑战第8天】 Linux的优点和缺点
746 6
|
Cloud Native 运维 Serverless
分布式应用的未来 — Distributionless
作者丨阿里云高级技术专家 至简(李云) 在技术变革推动社会发展这一时代背景下,大量支撑规模化分布式应用的技术创新、创造与创业应用而生,Could Native、Service Mesh、Serverless 等技术词汇在全球范围内引发了大量的解读与讨论。