美文网首页
状态封装布局

状态封装布局

作者: 记得写丶 | 来源:发表于2017-08-25 15:25 被阅读0次

状态封装布局

1.分析,我们加载一个页面它的显示样式有三种:正在加载、加载错误
、显示内容,那我们怎么去判断呢,所以我们需要在内部写一个方法来判断我们是应该显示那个页面。

2.我们更具分析结果,我们来看看我么将要去写的方法:

  • ProgressLayout(Context context){···}

  • ProgressLayout(Context context, @Nullable AttributeSet attrs){···}

  • ProgressLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr){···}(基本的构造方法)

  • init(AttributeSet attrs) (在这个方法中,我们拿到我们的布局)

  • onDetachedFromWindow()(该方法用于显示错误页面是,我们点击刷新页面,重绘页面)

  • public void showLoading(){···}暴露给外面可以使用的方法,显示我们的加载页面

  • public void showContent(){···}暴露给外面可以使用的方法,显示我们的内容页面

  • public void showError (OnClickListener click){···}暴露给外面可以使用的方法,显示我们的显示错误页面,值得注意的是我们在看到它的参数中有一个点击事件,用户在加载错误时,我们可以点击相对应的控件刷新我们的布局。

  • public boolean isContent(){···}该方法用于判断我们此时页面的状态,判断我们的页面是不是我们的内容状态,如果不是我们显示错误页。

  • showLoadingView(){···}这个方法就是加载方法的具体实现,用于添加我们的加载页面,并在这个加载页面中,我们给我们的控件设置动画。

  • showErrorView(){···}显示错误页面的具体实现,在这个方法中我们把我们的错误页面布局添加进来,同时,我们也应该把我们的错误页用户点击刷新的控件实例化出来,用于提取它的点击事件。

  • hideLoadingView(){···}用于隐藏我们的错误页面

  • hideLoadingView(){···}用于隐藏我们的加载页面

  • hideContentView(){···}用于隐藏我们的内容页

  • setContentVisibility (boolean visible){···}这个方法用于我们手动设置我们的内容页不能显示。

3.用于判断我们的状态我们可以使用我们的枚举方法来写入我么的三种状态,具体的代码我们来如下:

public enum State {
    LOADING,CONTENT,ERROR
}

4.具体的代码如下:

public class ProgressLayout extends LinearLayout{

private static final String LOADING_TAG = "loading_tag";
private static final String ERROR_TAG = "error_tag";

private LayoutParams layoutParams ;
private LayoutInflater layoutInflater;
private LinearLayout loadingView , errorView;

private TextView btn_error;

private List<View> contentViews = new ArrayList<>();
private RotateAnimation rotateAnimation;

private State currentState = State.LOADING;

public enum State {
    LOADING,CONTENT,ERROR
}

public ProgressLayout(Context context) {
    super(context);
}

public ProgressLayout(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    init(attrs);
}

public ProgressLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(attrs);
}

private void init(AttributeSet attrs) {
    layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
    super.addView(child, index, params);
    if(child.getTag() == null ||(!child.getTag().equals(LOADING_TAG))&&!(child.getTag().equals(ERROR_TAG))){
        contentViews.add(child);
    }
}

@Override
protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();
    if(btn_error != null){
        btn_error.setOnClickListener(null);
    }
}

public void showLoading(){
    currentState = State.LOADING;
    this.showLoadingView();
    this.hideErrorView();
    this.setContentVisibility(false);
}

public void showContent(){
    currentState = State.CONTENT;
    ProgressLayout.this.setContentVisibility(true);
    ProgressLayout.this.hideErrorView();
}

public void showError (OnClickListener click){
    currentState = State.ERROR;
    this.hideLoadingView();
    this.showErrorView();
    this.btn_error.setOnClickListener(click);
    hideContentView();
}

public boolean isContent(){
    return currentState == State.CONTENT;
}

private void showLoadingView(){
    if(loadingView == null){
        loadingView = (LinearLayout) layoutInflater.inflate(R.layout.layout_loading_view,null);
        loadingView.setTag(LOADING_TAG);
        layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        ImageView iv_loading = (ImageView) loadingView.findViewById(R.id.iv_loading);
        rotateAnimation = new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        rotateAnimation.setDuration(800);
        rotateAnimation.setRepeatMode(Animation.RESTART);
        rotateAnimation.setRepeatCount(Animation.INFINITE);
        rotateAnimation.start();
        LinearInterpolator lir = new LinearInterpolator();
        rotateAnimation.setInterpolator(lir);
        iv_loading.startAnimation(rotateAnimation);
        this.addView(loadingView,layoutParams);
    }else {
        rotateAnimation.start();
        loadingView.setVisibility(VISIBLE);
    }
}

private void showErrorView(){
    if(errorView == null){
        errorView = (LinearLayout) layoutInflater.inflate(R.layout.layout_error_view,null);
        errorView.setTag(ERROR_TAG);
        btn_error = (TextView) errorView.findViewById(R.id.btn_try);
        layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        this.addView(errorView,layoutParams);
    }else {
        errorView.setVisibility(VISIBLE);
    }
}

private void hideLoadingView(){
    if(loadingView != null && loadingView.getVisibility() != GONE){
        loadingView.setVisibility(GONE);
        rotateAnimation.cancel();
    }
}

private void hideErrorView(){
    if(errorView != null && errorView.getVisibility() != GONE){
        errorView.setVisibility(GONE);
    }
}

private void hideContentView(){
    if(contentViews != null){
        for(View contentView : contentViews){
            contentView.setVisibility(GONE);
        }
    }
}

public void setContentVisibility (boolean visible){
    for(View contentView :contentViews){
        contentView.setVisibility(visible ? View.VISIBLE:View.GONE);
    }
 }

}

相关文章

  • 状态封装布局

    状态封装布局 1.分析,我们加载一个页面它的显示样式有三种:正在加载、加载错误、显示内容,那我们怎么去判断呢,所以...

  • 直播项目笔记(一)

    颜色封装 + ClOPageView + 瀑布流 搭建主题框架 导航栏布局 改变导航栏的颜色 改变状态栏的颜色 设...

  • Masonry 学习笔记

    Masonry 用链式语句封装自动化布局,让布局的语句可以更简洁明了,可读性更高。首先将布局语句封装在 block...

  • 关于宝莲灯前台页面布局

    采用weAdmin封装的layui框架,重新布局页面。

  • rem布局封装

    rem 是 css 的长度单位,它是相对于 元素的 font-size 的相对值。这里根据设备宽度定义了1rem为...

  • 状态模式和策略模式的区别

    策略模式封装算法对象,由客户端决定用哪个算法,而状态模式封装状态对象,状态内部可以迁移,封装了迁移规则,是对状态的...

  • 使用Masonry代码进行屏幕适配的详细介绍

    Masonry自动布局使用 Masonry是一个轻量级的布局框架,采用更好的语法封装自动布局,它有自己的布局DSL...

  • 使用Masonry平分布局

    开发中经常遇到多个区块需要平分布局,下面列出Masonry自动布局中的平分布局的封装方法

  • BaseActivity封装

    1 封装点 1.封装抽象方法 初始化参数 initParams 获取布局资源Id getLayoutResID 初...

  • MeasureSpec 理解

    MeasureSpec 封装了父布局传递给子布局的布局要求,每个MeasureSpec代表了一组宽度和高度的要求 ...

网友评论

      本文标题:状态封装布局

      本文链接:https://www.haomeiwen.com/subject/mawudxtx.html