状态封装布局

状态封装布局

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);
    }
 }

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,902评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,037评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,978评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,867评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,763评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,104评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,565评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,236评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,379评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,313评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,363评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,034评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,637评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,719评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,952评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,371评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,948评论 2 341

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,566评论 18 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,275评论 25 707
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,537评论 18 399
  • 做选择时候的很多纠结,其实是各种复杂情绪在影响你的判断。旁观者清,其实就是因为旁观者没有你那么多复杂情绪,往往能做...
    给小老板读商业书阅读 151评论 0 1
  • (图片来自网络) 文|东水长盈 丁小可对着俞秋说道:“你手机号多少,我存下来。还有你微信,加一下我。” 俞秋报了一...
    东水长盈阅读 492评论 0 2