码迷,mamicode.com
首页 > 移动开发 > 详细

Android自定义View之LoadingLayout

时间:2017-02-23 19:06:44      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:iss   状态   bundle   img   margin   span   super   自定义view   ble   

介绍

上一篇博文写了一个通用的加载view,这篇在加载view的基础在包裹一层就是LoadingLayout了,主要的目的是免去每次加载时要隐藏主内容布局,然后加载成功之后显示主内容布局这些繁琐操作。如果你还不了解loadingView,可以简单的看一下上一篇博文:Android 自定义通用的loadingview,实现原理很简单,就是LoadingLayout在包裹内容层的基础上,在代码里添加loadingView作为第二个子view,所以不做过多讲解,大家看完直接下载源码参考。

LoadingLayout

This is a view for simplify the operation to loading

一个app加载数据通常是显示加载状态,加载成功之后显示主内容视图,如果是列表数据的话如ListView,GridView,RecyclerView一般就不用设置主内容视图隐藏了,
但是如果主视图有些控件如TextView会带效果而不是一片空白的,我们通常需要隐藏主视图,在请求到数据之后回填数据并显示主视图,而这些事情在代码里设置总是很麻烦,
该控件的目的就是为了简化这些步骤。

技术分享

特点

1、使用简单,实现原理也简单。

2、支持自定义各种视图,只需要把你要显示的视图set进去即可

技术分享

3、支持设置错误视图点击事件。

这里只是提供个思路,大家可以下载源码去修改成最适合你的view。

使用

1、xml里声明view,包裹在内容视图的外层。

<?xml version="1.0" encoding="utf-8"?>
<com.qiangyuyang.demo.widget.CommonLoadingLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/loadingLayout"
    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">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="主内容"/>
</com.qiangyuyang.demo.widget.CommonLoadingLayout>

2、Java代码里获取控件并在合适的时候调用加载,加载失败,加载成功等方法。


public class LoadingLayoutActivity extends AppCompatActivity {

    protected CommonLoadingLayout mLoadingLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_loading_layout);


        mLoadingLayout = (CommonLoadingLayout) findViewById(R.id.loadingLayout);

        //设置错误视图点击重新加载事件
        mLoadingLayout.setLoadingHandler(new CommonLoadingView.LoadingHandler() {
            @Override
            public void doRequestData() {
                mLoadingLayout.load();
                mLoadingLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mLoadingLayout.loadSuccess();
                    }
                }, 3000);
            }
        });

        //模拟加载网络请求后出现错误
        mLoadingLayout.load();
        mLoadingLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                mLoadingLayout.loadError();
            }
        }, 3000);

    }

}

3、自定义加载、加载错误、等视图。

        ProgressBar progressBar = new ProgressBar(this);
        this.mLoadingLayout.setLoadingView(progressBar);
        TextView textView = new TextView(this);
        textView.setText("加载失败...");
        this.mLoadingLayout.setLoadingErrorView(textView);

        mLoadingLayout.load();

源码下载

https://github.com/yissan/LoadingLayout

如果我的文章对你有帮助,请动动手指点个赞或关注,您的支持会是我永恒的动力。

Android自定义View之LoadingLayout

标签:iss   状态   bundle   img   margin   span   super   自定义view   ble   

原文地址:http://blog.csdn.net/yissan/article/details/56667538

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!