android介面空資料,沒網路的背景互動

pszh發表於2016-06-29

直接效果圖(這是模擬了一個沒有網路的時候得圖)

看下自定義的一個EmptyLayout:(來源於開源中國)因為沒什麼難度就直接貼程式碼了

package com.example.baselayoutapp;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
 * 
 * @author 開源中國
 *
 */
public class EmptyLayout extends LinearLayout implements android.view.View.OnClickListener {
	//載入成功 不顯示emptylayout了
	public static final int HIDE_LAYOUT = 4;
	//網路沒有連線
	public static final int NETWORK_ERROR = 1;
	//正在載入資料
	public static final int NETWORK_LOADING = 2;
	//沒有資料
	public static final int NODATA = 3;
	
	public static final int NODATA_ENABLE_CLICK = 5;

	private ProgressBar animProgress;
	private boolean clickEnable = true;
	private final Context context;
	public ImageView img;
	private android.view.View.OnClickListener listener;
	private int mErrorState;
	private String strNoDataContent = "";
	private TextView tv;

	public EmptyLayout(Context context) {
		super(context);
		this.context = context;
		init();
	}

	public EmptyLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init();
	}

	private void init() {
		View view = View.inflate(context, R.layout.view_error_layout, null);
		img = (ImageView) view.findViewById(R.id.img_error_layout);
		tv = (TextView) view.findViewById(R.id.tv_error_layout);
		RelativeLayout mLayout = (RelativeLayout) view.findViewById(R.id.pageerrLayout);
		animProgress = (ProgressBar) view.findViewById(R.id.animProgress);
		setBackgroundColor(-1);
		setOnClickListener(this);
		img.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (clickEnable) {
					// setErrorType(NETWORK_LOADING);
					if (listener != null)
						listener.onClick(v);
				}
			}
		});
		addView(view);
		changeErrorLayoutBgMode(context);
	}

	public void changeErrorLayoutBgMode(Context context1) {
		// mLayout.setBackgroundColor(SkinsUtil.getColor(context1,
		// "bgcolor01"));
		// tv.setTextColor(SkinsUtil.getColor(context1, "textcolor05"));
	}

	public void dismiss() {
		mErrorState = HIDE_LAYOUT;
		setVisibility(View.GONE);
	}

	public int getErrorState() {
		return mErrorState;
	}

	public boolean isLoadError() {
		return mErrorState == NETWORK_ERROR;
	}

	public boolean isLoading() {
		return mErrorState == NETWORK_LOADING;
	}

	@Override
	public void onClick(View v) {
		if (clickEnable) {
			// setErrorType(NETWORK_LOADING);
			if (listener != null)
				listener.onClick(v);
		}
	}

	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();
		onSkinChanged();
	}

	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
	}

	public void onSkinChanged() {
	}

	public void setErrorMessage(String msg) {
		tv.setText(msg);
	}

	/**
	 * 新添設定背景
	 * @param imgResource 圖片的id
	 * @param msg  圖片下面的textView顯示的文字
	 * 
	 */
	public void setErrorImag(int imgResource,String msg) {
		try {
			img.setBackgroundResource(imgResource);
			tv.setText(msg);
		} catch (Exception e) {
		}
	}

	public void setErrorType(int i) {
		setVisibility(View.VISIBLE);
		switch (i) {
		case NETWORK_ERROR:
			mErrorState = NETWORK_ERROR;
			if (isConnectivity(context)) {
				tv.setText(R.string.error_view_load_error_click_to_refresh);
				img.setBackgroundResource(R.drawable.pagefailed_bg);
			} else {
				tv.setText(R.string.error_view_network_error_click_to_refresh);
				img.setBackgroundResource(R.drawable.page_icon_network);
			}
			img.setVisibility(View.VISIBLE);
			animProgress.setVisibility(View.GONE);
			clickEnable = true;
			break;
		case NETWORK_LOADING:
			mErrorState = NETWORK_LOADING;
			animProgress.setVisibility(View.VISIBLE);
			img.setVisibility(View.GONE);
			tv.setText(R.string.error_view_loading);
			clickEnable = false;
			break;
		case NODATA:
			mErrorState = NODATA;
			img.setBackgroundResource(R.drawable.page_icon_empty);
			img.setVisibility(View.VISIBLE);
			animProgress.setVisibility(View.GONE);
			setTvNoDataContent();
			clickEnable = true;
			break;
		case HIDE_LAYOUT:
			setVisibility(View.GONE);
			break;
		case NODATA_ENABLE_CLICK:
			mErrorState = NODATA_ENABLE_CLICK;
			img.setBackgroundResource(R.drawable.page_icon_empty);
			img.setVisibility(View.VISIBLE);
			animProgress.setVisibility(View.GONE);
			setTvNoDataContent();
			clickEnable = true;
			break;
		default:
			break;
		}
	}

	public void setNoDataContent(String noDataContent) {
		strNoDataContent = noDataContent;
	}

	public void setOnLayoutClickListener(View.OnClickListener listener) {
		this.listener = listener;
	}

	public void setTvNoDataContent() {
		if (!strNoDataContent.equals(""))
			tv.setText(strNoDataContent);
		else
			tv.setText(R.string.error_view_no_data);
	}

	@Override
	public void setVisibility(int visibility) {
		if (visibility == View.GONE)
			mErrorState = HIDE_LAYOUT;
		super.setVisibility(visibility);
	}

	
	
	
	/**
	 * 
	 * 描述:是否有網路連線.androidbase中AbWifiUtil中的方法
	 * @param context
	 * @return
	 */
	public static boolean isConnectivity(Context context) {
		
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		TelephonyManager telephonyManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		return ((connectivityManager.getActiveNetworkInfo() != null && connectivityManager
				.getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || telephonyManager
				.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
	}
	
}

然後就是activity中的使用了:

private void initView() {
		mErrorLayout = (EmptyLayout) findViewById(R.id.empty_view);
		mListView = (ListView) findViewById(R.id.list);
		mErrorLayout.setOnLayoutClickListener(new View.OnClickListener() {
	            @Override
	            public void onClick(View v) {
	                virtulHttp();
	            }
		 });
		
	}
	//模擬網路請求資料
	private  void virtulHttp(){
		mErrorLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(1000);
					if(EmptyLayout.isConnectivity(MainActivity.this)){//有沒有網
						data = new String[]{"啦","啦啦","啦啦啦"};
						adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_expandable_list_item_1, android.R.id.text1,data );
					}else{
						data = new String[]{};
					}
					mHandler.sendEmptyMessage(0);
					
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}).start();
	}
	Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(null!=adapter){
				mListView.setAdapter(adapter);
				mErrorLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
			} else if (0==data.length){
				if(EmptyLayout.isConnectivity(MainActivity.this)){//網路連線問題
					mErrorLayout.setErrorType(EmptyLayout.NODATA);
					return;
				}
				mErrorLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
			}
		};
	};
佈局檔案是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

   
  <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         />
   <com.example.baselayoutapp.EmptyLayout
        android:id="@+id/empty_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</RelativeLayout>

最後貼下demo下載地址:http://www.oschina.net/code/snippet_2702417_57579

相關文章