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

2017-12-18  本文已影响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);}}

上一篇 下一篇

猜你喜欢

热点阅读