Android网络请求失败的加载页面

今天我们来研究网络加载中设计的加载界面

效果图:

Android网络请求失败的加载页面_第1张图片
2016-10-18-06mzload.gif
我们先实现实现加载页面,这里我们继承FrameLayout ,主要就是在网络加载和失败覆盖在我们需要显示的页面上面就ok!

LoadingFramelayout.java

public class LoadingFramelayout extends FrameLayout {    
        private ImageView mIvLoading;    
        private Button mBtnReTry;   
        private RelativeLayout mLoadingView;    
        private AnimationDrawable mAnimationDrawable;   
        private OnReloadListener mOnReloadListener;    
         //构造方法,在我们需要网络加载页面是调用,看清楚,第二个传入的一个布局
        public LoadingFramelayout(Context context, @LayoutRes int res) {       
                  super(context);                                                                 
                  LayoutInflater mInflater = LayoutInflater.from(context); 
                  mInflater.inflate(res,this);                                                     
                  View rootView = mInflater.inflate(R.layout.fragment_loading_framelayout,this);        
                  mLoadingView = (RelativeLayout) rootView.findViewById(R.id.load_view);        
                  mIvLoading = (ImageView) rootView.findViewById(R.id.iv_loading);      
                  mBtnReTry = (Button) rootView.findViewById(R.id.btn_retry);                                                           
                  mBtnReTry.setOnClickListener(new OnClickListener() {    
                        @Override
                         public void onClick(View view) { 
                               mOnReloadListener.onReload();            
                        }        
                  });        
                init(context);    
          } 
          // 构造方法,在我们需要网络加载页面是调用,看清楚,第二个传入的一个View
          public LoadingFramelayout(Context context,View view) {  
                  super(context);        
                  addView(view);        
                  LayoutInflater mInflater = LayoutInflater.from(context);        
                  View rootView = mInflater.inflate(R.layout.fragment_loading_framelayout,this);        
                  mLoadingView = (RelativeLayout) rootView.findViewById(R.id.load_view);        
                  mIvLoading = (ImageView) rootView.findViewById(R.id.iv_loading); 
                  mBtnReTry = (Button) rootView.findViewById(R.id.btn_retry);   
                  mBtnReTry.setOnClickListener(new OnClickListener() {            
                         @Override           
                         public void onClick(View view) {                
                                    mOnReloadListener.onReload();            
                         }        
                 });        
                  init(context);    
            }   
             //这里是一个动画效果,我们这里采用6张不同加载时候的图片 
            private void init(Context context) {        
                         mAnimationDrawable = new AnimationDrawable();        
                         mAnimationDrawable.setOneShot(false);
                         mAnimationDrawable.addFrame(ContextCompat.getDrawable(context,R.drawable.loading_01),100);
   mAnimationDrawable.addFrame(ContextCompat.getDrawable(context,R.drawable.loading_02),100);

       mAnimationDrawable.addFrame(ContextCompat.getDrawable(context,R.drawable.loading_03),100); 
                          mAnimationDrawable.addFrame(ContextCompat.getDrawable(context,R.drawable.loading_04),100);
                          mAnimationDrawable.addFrame(ContextCompat.getDrawable(context,R.drawable.loading_05),100);       
                          mAnimationDrawable.addFrame(ContextCompat.getDrawable(context,R.drawable.loading_06),100);
    
                          mIvLoading.setImageDrawable(mAnimationDrawable);      
                          mIvLoading.post(new Runnable() {            
                              @Override            
                              public void run() {                
                                      mAnimationDrawable.start();            
                              }       
                         });    
              }  
      //这一个用于完成加载的调用方法  
      public void completeLoading(){
                stopLoading();        
                post(new Runnable() {                
                       @Override           
                        public void run() {                
                                mLoadingView.setVisibility(GONE);            
                         }        
                });    
    } 
    //成功和失败后停着加载,将动画stop   
    private void stopLoading() {   
             ((AnimationDrawable)mIvLoading.getDrawable()).stop();    
    }
    //在加载失败后,点击加载再次加载的时候调用    
    public void startLoading(){       
             post(new Runnable() {            
                    @Override            
                    public void run() {                
                              mBtnReTry.setVisibility(GONE);          
                              mIvLoading.setImageDrawable(mAnimationDrawable);     
                             ((AnimationDrawable)mIvLoading.getDrawable()).start();         
                 }        
            });    
    }    
    //加载失败后,停着动画,将失败页面和重新加载按钮Visible出来
    public void loadingFailed(){        
              stopLoading();        
              post(new Runnable() {            
                        @Override           
                         public void run() {   
                             mIvLoading.setImageResource(R.drawable.loading_failed);     
                             mBtnReTry.setVisibility(VISIBLE);           
                       }        
            });    
  }
   //在重新加载的时候用来回调重新加载的方法    
  public void setOnReloadListener(OnReloadListener onReloadListener) {     
               mOnReloadListener = onReloadListener;    
   }
  //实现接口,重新加载用作回调    
  public interface OnReloadListener {        
            void onReload();    
    }
}

MainActivity.java作为一个开始请求网络的发起页面

Android网络请求失败的加载页面_第2张图片
device-2016-10-17-214400.png
   public class MainActivity extends Activity {    
        @Override    
        protected void onCreate(Bundle savedInstanceState) {  
              super.onCreate(savedInstanceState);       
              setContentView(R.layout.activity_main);        
              init();    
        }    
        private void init() {
             //监听按钮,进入下一页 
              findViewById(R.id.btn_load_net).setOnClickListener(new View.OnClickListener() {            
                @Override            
                public void onClick(View view) {                
                      Intent intent = new Intent(MainActivity.this,NetWorkActivity.class);
                      startActivity(intent);            
                 }        
          });    
      }
}

NetWorkActivity.java 使我们用来模拟网络请求成功和失败

加载成功后的界面
Android网络请求失败的加载页面_第3张图片
device-2016-10-17-214215.png
public class NetWorkActivity extends Activity {    
          private LoadingFramelayout mLoadingFramelayout;    
          @Override    
          protected void onCreate(Bundle savedInstanceState) {  
                  super.onCreate(savedInstanceState);        
                  mLoadingFramelayout = newLoadingFramelayout(this,R.layout.activity_net_work);        
                setContentView(mLoadingFramelayout);        
                init();    
          }    
          private void init() {        
                simulation2NetworkFialed();    
          }    
          //模拟网络加载失败
          private void simulation2NetworkFialed(){  
                   //开启一个子线程       
                  new Thread(new Runnable() {            
                          @Override            
                           public void run() {                
                                  try {                    
                                          Thread.sleep(2000);//这里我们模拟网络请求休眠2s                    
                                           mLoadingFramelayout.loadingFailed(); 
                                           mLoadingFramelayout.setOnReloadListener(new LoadingFramelayout.OnReloadListener() {                        
                                                    @Override                        
                                                    public void onReload() {      
                                                            mLoadingFramelayout.startLoading();         
                                                           simulation2Network();                        
                                                     }                   
                                              });                
                                      } catch (InterruptedException e) {  
                                                e.printStackTrace();                
                                       }            
                      }        
              }).start();   
         }   
          //网络请求通畅
         private void simulation2Network(){
                  //开启一个子线程        
                  new Thread(new Runnable() {            
                          @Override           
                           public void run() {                
                                     try {                    
                                               Thread.sleep(2000); //这里模拟再次请求网络  
                                                mLoadingFramelayout.completeLoading();             
                                       } catch (InterruptedException e) {       
                                               e.printStackTrace();               
                                       }            
                          }        
          }).start();    
      }
}

你可能感兴趣的:(Android网络请求失败的加载页面)