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);}}
网络请求中的四种状态之间相互切换
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 前言 在Android开发中我们经常遇到这样的场景:去加载网络数据时需要显示进度条,提示用户正在加载,加载失败需要...
- 环信官方Demo源码分析及SDK简单应用 环信官方Demo源码分析及SDK简单应用-ChatDemoUI3.0 环...
- 版权声明:本文为Yiwent原创文章,转载必须注明出处。 1.登陆界面 对于一个网络APP来说,刚开始首先必须登录...