自定义页面加载LoadingLayout

做项目当中总是遇到页面数据加载的情况,自定义了几个加载情况的xml布局,例如加载失败,加载数据为空,加载成功,正在加载等,但是发现每次都需要根据加载情况去处理显示哪种xml,很麻烦,也很容易出错,所以我就想以自定义组合控件的方式来处理,达到复用的目的,代码可优化的地方有很多,大家可以根据自己的需求做修改。
首先自定义属性了:

    <!--loadinglayout-->
    <declare-styleable name="LoadingLayout">
        <attr name="loadingLayoutId" format="reference"/>
        <attr name="failureLayoutId" format="reference"/>
        <attr name="emptyLayoutId" format="reference"/>
        <attr name="contentId" format="reference"/>
    </declare-styleable>

代码很简单,自己看看

public class LoadingLayout extends FrameLayout {


    private View loadingView, failureView, emptyView, contentView;

    private RetryListener retryListener;

    public LoadingLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadingLayout(Context context) {
        this(context, null);
    }

    public LoadingLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LoadingLayout, defStyleAttr, 0);

        int loadingId = typedArray.getResourceId(R.styleable.LoadingLayout_loadingLayoutId, 0);

        int failureId = typedArray.getResourceId(R.styleable.LoadingLayout_failureLayoutId, 0);

        int emptyId = typedArray.getResourceId(R.styleable.LoadingLayout_emptyLayoutId, 0);

        int contentId = typedArray.getResourceId(R.styleable.LoadingLayout_contentId, 0);

        typedArray.recycle();

        initView(context, loadingId, failureId, emptyId, contentId);

    }

    //初始化三个状态View
    private void initView(Context context, int loadingId, int failureId, int emptyId, int contentId) {

        createChildView(context, loadingId);
        createChildView(context, failureId);
        createChildView(context, emptyId);
        createChildView(context, contentId);
    }


    //创建子视图
    private void createChildView(Context context, int Id) {

        if (Id!=0) {

            View.inflate(context,Id, this);
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        //获取显示内容区域
        if (getChildCount() > 3) {
            loadingView = getChildAt(0);
            failureView = getChildAt(1);
            emptyView = getChildAt(2);
            contentView = getChildAt(3);
            contentView.setVisibility(GONE);
            if (loadingView != null) {

                loadingView.setVisibility(GONE);
            }

            if (failureView != null) {

                failureView.setVisibility(GONE);
                View rButton = (failureView.findViewById(R.id.tv_retry) == null) ? failureView : failureView.findViewById(R.id.tv_retry);
                rButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        if (retryListener != null) {


                            retryListener.retryClick();


                        }
                    }
                });
            }

            if (emptyView != null) {

                emptyView.setVisibility(GONE);
            }

        }


    }


    //设置重试监听
    public void setRetryListener(RetryListener retryListener) {
        this.retryListener = retryListener;
    }

    //显示加载view
    public void showLoading() {

        show(1);

    }


    //显示失败view
    public void showFailure() {

        show(2);

    }


    //显示空view
    public void showEmpty() {

        show(3);

    }


    //显示空view
    public void showContent() {

        show(4);
    }


    //根据id展示布局
    private void show(int id) {

        if (loadingView != null) {

            loadingView.setVisibility(id == 1 ? VISIBLE : GONE);
        }

        if (failureView != null) {

            failureView.setVisibility(id == 2 ? VISIBLE : GONE);

        }

        if (emptyView != null) {

            emptyView.setVisibility(id == 3 ? VISIBLE : GONE);
        }

        if (contentView != null) {

            contentView.setVisibility(id == 4 ? VISIBLE : GONE);
        }
    }


    public interface RetryListener {

        void retryClick();

    }

}

然后就是如何使用了

<?xml version="1.0" encoding="utf-8"?>
<com.yjjy.app.view.LoadingLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/loadingLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:loadingLayoutId="@layout/progressdialoag"
    app:failureLayoutId="@layout/loading_failure"
    <!--可以在这里引用设置contentlayout-->
    >
        <!--也可以在这里引用设置contentlayout-->
</com.yjjy.app.view.LoadingLayout>

然后在使用的地方调用下面几个方法即可:

    //显示加载view
    public void showLoading() {

        show(1);

    }


    //显示失败view
    public void showFailure() {

        show(2);

    }


    //显示空view
    public void showEmpty() {

        show(3);

    }


    //显示空view
    public void showContent() {

        show(4);
    }

程序员内功修炼手册 不定期分享程序员基础知识,大前端知识!想跟博主一块成长的快快关注吧!

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
前言项目里都会遇到几种页面,分别为加载中、无网络、无数据、出错四种情况,经常要使用,所以封成库引用了,方便使用,顺便分享出来。先看一下效果: 原理比较简单,继承FrameLayout,在xml渲染完成后,加上加载中、无网络、无数据、出错四个页面,根据需要控制显示哪一层,花了些时间,开了很多方法出来,支持很多属性的设置,算是比较实用,源码里已对各个方法的作用都加了注释,就不做过多解释了,项目GitHub地址:https://github.com/weavey/LoadingLayoutDemo,感兴趣的可以看看,欢迎指出问题。使用方式gradle引用:compile 'com.lai.weavey:loadinglayout:1.2'使用说明LoadingLayout支持全局配置,对所有使用到的地方都起效,需要在Application中配置,如下: public class App extends Application {     @Override     public void onCreate() {         super.onCreate();         LoadingLayout.getConfig()                 .setErrorText("出错啦~请稍后重试!")                 .setEmptyText("抱歉,暂无数据")                 .setNoNetworkText("无网络连接,请检查您的网络···")                 .setErrorImage(R.mipmap.define_error)                 .setEmptyImage(R.mipmap.define_empty)                 .setNoNetworkImage(R.mipmap.define_nonetwork)                 .setAllTipTextColor(R.color.gray)                 .setAllTipTextSize(14)                 .setReloadButtonText("点我重试哦")                 .setReloadButtonTextSize(14)                 .setReloadButtonTextColor(R.color.gray)                 .setReloadButtonWidthAndHeight(150,40);     } }由于“加载中”的页面,可能每个App都不一样,因此,LoadingLayout支持自定义LoadingPage,如下: LoadingLayout.getConfig()      .setLoadingPageLayout(R.layout.define_loading_page);同时,为了适应个别界面的“特殊需求”,LoadingLayout也支持局部设置各种属性,仅对当前对象生效,不影响全局。如下:        LoadingLayout  loading = (LoadingLayout) findViewById(R.id.loading_layout);         loading.setLoadingPage(R.layout.define_loading_page)                 .setEmptyText("暂无报告数据")                 .setErrorText("")                 .setNoNetworkText("")                 .setErrorImage(R.mipmap.ic_launcher)                 .setErrorTextSize(16)                 .setReloadButtonText("点我重新加载哦"); //等等为ReloadButton设置监听:loadingLayout.setOnReloadListener(new LoadingLayout.OnReloadListener() {             @Override             public void onReload(View v) {             }         });设置显示的页面: loadingLayout.setStatus(LoadingLayout.Loading);//加载中  loadingLayout.setStatus(LoadingLayout.Empty);//无数据  loadingLayout.setStatus(LoadingLayout.Error);//错误  loadingLayout.setStatus(LoadingLayout.No_Network);//无网络  loadingLayout.setStatus(LoadingLayout.Success);//加载成功最后,在xml里面使用:<com.weavey.loading.lib.LoadingLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingBottom="@dimen/activity_vertical_margin"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     app:isFirstVisible="true">     <TextView         android:background="@color/colorPrimary"         android:visibility="visible"         android:gravity="center"         android:textColor="@android:color/white"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:text="ContentView"/> </com.weavey.loading.lib.LoadingLayout>注意: (1)isFirstVisible属性用来控制contentView一开始是否隐藏,由于LoadingLayout原理是在xml渲染完成后在contentView上铺上三层View,因此,一开始如果不隐藏,等contentView渲染完成后调用: loadingLayout.setStatus(LoadingLayout.Loading); 会造成界面闪烁的效果,影响体验,因此默认将contentView隐藏,所以数据加载完成后一定要调用loadingLayout.setStatus(LoadingLayout.Success);,将contentView显示出来。这样也能解决未获取到数据的情况下,被用户看到杂乱无章的布局,个人还是比较喜欢默认隐藏contentView; (2)为了方便管理,LoadingLayout只能有一个直属子View,类似ScrollView,添加两个直属子View会抛出异常throw new IllegalStateException("LoadingLayout can host only one direct child");; (3)由于AS会直接将自定义View的特性反应在预览界面,所以在使用LoadingLayout的时候,会无法看到被LoadingLayout包裹住的布局(默认为gone),因此也可以将isFirstVisible属性暂时设为true,预览布局

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值