废话不多说,直接上代码。
import android.content.Context;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* Created by chenxiaoli on 2017/3/18 0018.
* 网络状态显示控制类
* 在布局文件中只能包含一个子类
* 可应用于activity,fragment,或者独立子布局中
*/
public class MyFramelayout extends FrameLayout {
private View successView;
private View loadingView;
private TextView emptyView;
private View errorView;
private OnReloadingListener listener;
public void setOnReloadingListener(OnReloadingListener listener) {
this.listener = listener;
}
public interface OnReloadingListener {
void onReloading();
}
public MyFramelayout(Context context, View loadingView, TextView emptyView, View errorView) {
super(context);
initView(context, loadingView, emptyView, errorView);
}
public MyFramelayout(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public MyFramelayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
/**
* 是否有网络连接
* @param context
* @return
*/
public boolean isNetworkConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable();
}
}
return false;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (successView == null && getChildCount() > 3) {
successView = getChildAt(3);
successView.setVisibility(INVISIBLE);
if (getChildCount() > 4) {
throw new RuntimeException("MyFramelayout在XML文件中只能存在一个子View");
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
private void initView(Context context) {
loadingView = LayoutInflater.from(context).inflate(R.layout.item_loading, this, false);
emptyView = (TextView) LayoutInflater.from(context).inflate(R.layout.item_load_empty, this, false);
errorView = LayoutInflater.from(context).inflate(R.layout.item_load_error, this, false);
errorView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.onReloading();
}
onLoading();
}
});
addView(loadingView);
addView(emptyView);
addView(errorView);
if(isNetworkConnected(context)){
onError();
Toast.makeText(context,"请打开网络连接",Toast.LENGTH_SHORT).show();
}else {
onLoading();
}
}
private void initView(Context context, View loadingView, TextView emptyView, View errorView) {
this.loadingView =loadingView;
this.emptyView = emptyView;
this.errorView = errorView;
errorView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.onReloading();
}
onLoading();
}
});
addView(loadingView);
addView(emptyView);
addView(errorView);
onLoading();
}
public void setLoadingView(View loadingView) {
this.loadingView = (LinearLayout) loadingView;
removeViewAt(0);
addView(loadingView, 0);
}
public void setEmptyView(TextView emptyView) {
this.emptyView = emptyView;
removeViewAt(1);
addView(emptyView, 1);
}
public void setErrorView(View errorView) {
this.errorView = errorView;
errorView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.onReloading();
}
onLoading();
}
});
removeViewAt(2);
addView(errorView, 2);
}
public void onLoading() {
if (successView != null) {
successView.setVisibility(INVISIBLE);
}
loadingView.setVisibility(VISIBLE);
emptyView.setVisibility(INVISIBLE);
errorView.setVisibility(INVISIBLE);
}
public void onError() {
if (successView != null) {
successView.setVisibility(INVISIBLE);
}
loadingView.setVisibility(INVISIBLE);
emptyView.setVisibility(INVISIBLE);
errorView.setVisibility(VISIBLE);
}
public void onSuccess() {
if (successView != null) {
successView.setVisibility(VISIBLE);
}
loadingView.setVisibility(INVISIBLE);
emptyView.setVisibility(INVISIBLE);
errorView.setVisibility(INVISIBLE);
}
public void onEmpty(String msg) {
if (successView != null) {
successView.setVisibility(INVISIBLE);
}
loadingView.setVisibility(INVISIBLE);
emptyView.setVisibility(VISIBLE);
emptyView.setText(msg);
errorView.setVisibility(INVISIBLE);
}
}
item_loading
你也可以定义自己的三种子控件,非常灵活
然后就是acitiviy中如何使用他
package com.ftoul.myapplication.activitys;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.xxx.myapplication.ui.MyFramelayout;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;
import okhttp3.Call;
import okhttp3.Response;
public class DemoActivity extends AppCompatActivity {
private MyFramelayout myFramelayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo);
myFramelayout = (MyFramelayout) findViewById(R.id.myframelayout);
myFramelayout.setOnReloadingListener(new MyFramelayout.OnReloadingListener() {
@Override
public void onReloading() {
getData();
}
});
getData();
}
/**
* 请求网络的方法
*/
private void getData(){
String datas = "";//请求网络的json数据
//这里以张宏洋的OkHttp为例子
OkHttpUtils
.postString()
.tag(this)
.url(MyURL.APP_SERVICE)//服务器地址
.content(datas)
.build()
.execute(new Callback() {//请求网络的回调中
@Override
public Object parseNetworkResponse(Response response, int id) throws Exception {//成功的回调
myFramelayout.onSuccess();
//开始解析数据
if(数据为空){
myFramelayout.onEmpty("暂无数据");
}
return null;
}
@Override
public void onError(Call call, Exception e, int id) {//错误回调中
myFramelayout.onError();
}
@Override
public void onResponse(Object response, int id) {
}
});
}
}
对应的xml文件如下
以上