美文网首页
网络请求中的四种状态之间相互切换

网络请求中的四种状态之间相互切换

作者: 张克同学 | 来源:发表于2017-12-18 17:32 被阅读0次

packagecom.tjhq.mygson.view;importandroid.content.Context;importandroid.text.TextUtils;importandroid.util.AttributeSet;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.Button;importandroid.widget.ImageView;importandroid.widget.RelativeLayout;importandroid.widget.TextView;importcom.tjhq.mygson.R;importjava.util.ArrayList;importjava.util.List;/*** Created by Mersens on 2016/10/2.* 用于网络请求中的四种状态之间相互切换*/publicclassProgressStateLayoutextendsRelativeLayout{privateLayoutParamslayoutParams;privateLayoutInflaterinflater;privateListviews=null;privatestaticfinalStringTAG_LOADING="loading";privatestaticfinalStringTAG_EMPTY="empty";privatestaticfinalStringTAG_ERROR="error";privateViewviewLoading,viewEmpty,viewError;privateReloadListenerlistener;//重新加载按钮的接口,用于监听重新加载按钮的监听回调publicinterfaceReloadListener{voidonClick();}privateenumType {LOADING,EMPTY,CONTENT,ERROR;}publicProgressStateLayout(Context context){this(context,null);}publicProgressStateLayout(Context context,AttributeSet attrs){this(context,attrs,0);}publicProgressStateLayout(Context context,AttributeSet attrs,intdefStyleAttr){super(context,attrs,defStyleAttr);init();}/*** 初始化操作*/publicvoidinit(){views=newArrayList();inflater= (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);layoutParams=newLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);layoutParams.addRule(CENTER_IN_PARENT);}@OverridepublicvoidaddView(View child,ViewGroup.LayoutParams params){super.addView(child,params);//把ProgressStateView内的子控件内容添加到list集合中,保证不同状态间相互切换内容的隐藏与显示if(child.getTag() ==null|| (!child.getTag().equals(TAG_LOADING) && !child.getTag().equals(TAG_EMPTY) && !child.getTag().equals(TAG_ERROR))) {views.add(child);} }/*** 改变状态方法*@paramtype*/privatevoidswitchState(Type type,intresid,String title,String msg,String btntext){switch(type) {caseLOADING: hideEmptyView();hideErrorView();setContentView(false);setLoadingView();break;caseEMPTY: hideErrorView();hideLoadingView();setContentView(false);setEmptyView(resid,title);break;caseERROR: hideEmptyView();hideLoadingView();setContentView(false);setErrorView(resid,title,msg,btntext);break;caseCONTENT: hideEmptyView();hideLoadingView();hideErrorView();setContentView(true);break;} }privatevoidsetLoadingView(){if(viewLoading==null) {viewLoading=inflater.inflate(R.layout.layout_loading,null);viewLoading.setTag(TAG_LOADING);viewLoading.requestLayout();addView(viewLoading,layoutParams);}else{viewLoading.setVisibility(View.VISIBLE);} }privatevoidsetEmptyView(intresid,String msg){if(viewEmpty==null) {viewEmpty=inflater.inflate(R.layout.layout_empty,null);if(resid !=0) { ImageView imageView = (ImageView)viewEmpty.findViewById(R.id.img_nodata);imageView.setImageResource(resid);}if(!TextUtils.isEmpty(msg)) { TextView tv_msg = (TextView) findViewById(R.id.text_nodata_tips);tv_msg.setText(msg);}viewEmpty.setTag(TAG_EMPTY);viewEmpty.requestLayout();addView(viewEmpty,layoutParams);}else{viewEmpty.setVisibility(View.VISIBLE);} }privatevoidsetErrorView(intresid,String title,String msg,String btntext){if(viewError==null) {viewError=inflater.inflate(R.layout.layout_error,null);if(resid !=0) { ImageView img = (ImageView) findViewById(R.id.img_nodata);img.setImageResource(resid);}if(!TextUtils.isEmpty(title)) { TextView tv_title = (TextView) findViewById(R.id.tv_title);tv_title.setText(title);}if(!TextUtils.isEmpty(msg)) { TextView tv_msg = (TextView) findViewById(R.id.tv_msg);tv_msg.setText(title);} Button btn_reload = (Button)viewError.findViewById(R.id.btn_reload);if(!TextUtils.isEmpty(btntext)) { btn_reload.setText(btntext);} btn_reload.setOnClickListener(newOnClickListener() {@OverridepublicvoidonClick(View v){if(listener!=null) {listener.onClick();} } });viewError.requestLayout();viewError.setTag(TAG_ERROR);addView(viewError,layoutParams);}else{viewError.setVisibility(View.VISIBLE);} }privatevoidhideLoadingView(){if(viewLoading!=null) {viewLoading.setVisibility(View.GONE);} }privatevoidhideEmptyView(){if(viewEmpty!=null) {viewEmpty.setVisibility(View.GONE);} }privatevoidhideErrorView(){if(viewError!=null) {viewError.setVisibility(View.GONE);} }privatevoidsetContentView(booleanflag){for(View v :views) { v.setVisibility(flag ? View.VISIBLE: View.GONE);} }publicvoidshowLoading(){ switchState(Type.LOADING,0,null,null,null);}publicvoidshowError(intresid,String title,String msg,String btntext,ReloadListener listener){this.listener= listener;switchState(Type.ERROR,resid,title,msg,btntext);}publicvoidshowEmpty(intresid,String msg){ switchState(Type.EMPTY,resid,msg,null,null);}publicvoidshowContent(){ switchState(Type.CONTENT,0,null,null,null);}publicvoidshowError(ReloadListener listener){this.listener= listener;switchState(Type.ERROR,0,null,null,null);}publicvoidshowEmpty(){ switchState( Type.EMPTY,0,null,null,null);}}

相关文章

  • 网络请求中的四种状态之间相互切换

    packagecom.tjhq.mygson.view;importandroid.content.Context...

  • Activity生命周期

    一、Activity的四种状态 Activity最大的特点是拥有多种状态,可以在多种状态之间切换,以此来控制自己的...

  • Android Switch禁止手动切换状态

    Switch的原有特性导致在网络请求数据还未成功时,就已经切换了状态,可能与真实状态不符合,所以需要关闭手动切换的...

  • OkHttp3中的HTTP/2首部压缩

    当前网络环境中,同一个页面发出几十个HTTP请求已经是司空见惯的事情了。在HTTP/1.1中,请求之间完全相互独立...

  • Alamofire 4.5.0源码解析-Request与Resp

    Alamofire中Request和Response是关于网络请求请求响应,Request有四种类型data,do...

  • Docker容器(宿主机应用)相互访问

    Docker容器(宿主机应用)之间的相互访问和使用的网络模式有关。关于Docker的四种网络模式这里不做详细介绍,...

  • 优雅地处理网络请求的依赖关系

    处理网络时,经常会遇到网络请求相互依赖的情况,如B请求的参数依赖A请求的回调结果,直接在A请求的回调中请求B,会导...

  • 如何在微服务之间调用时传递token

    当我们在服务之进行相互调用时,由于后台服务是无状态的,因此,我们通常需要在服务之间相互调用时,在请求中携带toke...

  • Rxjava实际应用场景

    Scheduler线程切换——eg:后台线程取数据,主线程展示 CheckBox状态实时更新 输入框改变即网络请求...

  • 进程调度

    需要调度的四种情况:1.进程从运行状态切换到等待状态2.进程从运行状态切换到就绪状态3.从等待状态切换到就绪状态4...

网友评论

      本文标题:网络请求中的四种状态之间相互切换

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