前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >5.代码抽取(加载界面)

5.代码抽取(加载界面)

作者头像
六月的雨
发布2018-05-14 11:33:18
7580
发布2018-05-14 11:33:18
举报
文章被收录于专栏:Android开发指南Android开发指南

简单框架的搭建主要就是泛型T和抽象类(让子类去实现)的运用

抽取3中的代码,加载界面逻辑是一样的分别抽取到 BaseFragment和LoadingPage中。自定义布局如果不在布局写,那就直接new就可以

LoadingPage是自定义的帧布局

public abstract class LoadingPage extends FrameLayout {	public static final int STATE_UNKOWN = 0;	public static final int STATE_LOADING = 1;	public static final int STATE_ERROR = 2;	public static final int STATE_EMPTY = 3;	public static final int STATE_SUCCESS = 4;	public int state = STATE_UNKOWN;	private View loadingView;// 加载中的界面	private View errorView;// 错误界面	private View emptyView;// 空界面	private View successView;// 加载成功的界面	public LoadingPage(Context context) {		super(context);		init();	}	public LoadingPage(Context context, AttributeSet attrs, int defStyle) {		super(context, attrs, defStyle);		init();	}	public LoadingPage(Context context, AttributeSet attrs) {		super(context, attrs);		init();	}	private void init() {		loadingView = createLoadingView(); // 创建了加载中的界面		if (loadingView != null) {			this.addView(loadingView, new FrameLayout.LayoutParams(					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));		}		errorView = createErrorView(); // 加载错误界面		if (errorView != null) {			this.addView(errorView, new FrameLayout.LayoutParams(					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));		}		emptyView = createEmptyView(); // 加载空的界面		if (emptyView != null) {			this.addView(emptyView, new FrameLayout.LayoutParams(					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));		}		showPage();// 根据不同的状态显示不同的界面	}	// 根据不同的状态显示不同的界面	private void showPage() {		if (loadingView != null) {			loadingView.setVisibility(state == STATE_UNKOWN					|| state == STATE_LOADING ? View.VISIBLE : View.INVISIBLE);		}		if (errorView != null) {			errorView.setVisibility(state == STATE_ERROR ? View.VISIBLE					: View.INVISIBLE);		}		if (emptyView != null) {			emptyView.setVisibility(state == STATE_EMPTY ? View.VISIBLE					: View.INVISIBLE);		}		if (state == STATE_SUCCESS) {			if (successView == null) {				successView = createSuccessView();				this.addView(successView, new FrameLayout.LayoutParams(						LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));			}			successView.setVisibility(View.VISIBLE);		} else {			if (successView != null) {				successView.setVisibility(View.INVISIBLE);			}		}	}	/* 创建了空的界面 */	private View createEmptyView() {		View view = View.inflate(UiUtils.getContext(), R.layout.loadpage_empty,				null);		return view;	}	/* 创建了错误界面 */	private View createErrorView() {		View view = View.inflate(UiUtils.getContext(), R.layout.loadpage_error,				null);		Button page_bt = (Button) view.findViewById(R.id.page_bt);		page_bt.setOnClickListener(new OnClickListener() {			@Override			public void onClick(View v) {				show();			}		});		return view;	}	/* 创建加载中的界面 */	private View createLoadingView() {		View view = View.inflate(UiUtils.getContext(),				R.layout.loadpage_loading, null);		return view;	}	public enum LoadResult {		error(2), empty(3), success(4);		int value;		LoadResult(int value) {			this.value = value;		}		public int getValue() {			return value;		}	}	// 根据服务器的数据 切换状态	public void show() {		if (state == STATE_ERROR || state == STATE_EMPTY) {			state = STATE_LOADING;		}		// 请求服务器 获取服务器上数据 进行判断		// 请求服务器 返回一个结果		ThreadManager.getInstance().createLongPool().execute(new Runnable() {						@Override			public void run() {				SystemClock.sleep(2000);				final LoadResult result = load();				UiUtils.runOnUiThread(new Runnable() {					@Override					public void run() {						if (result != null) {							state = result.getValue();							showPage(); // 状态改变了,重新判断当前应该显示哪个界面						}					}				});			}		});						showPage();	}	/***	 * 创建成功的界面	 * 	 * @return	 */	public abstract View createSuccessView();	/**	 * 请求服务器	 * 	 * @return	 */	protected abstract LoadResult load();}

在uiutils添加这个方法

	/**	 * 把Runnable 方法提交到主线程运行	 * @param runnable	 */	public static void runOnUiThread(Runnable runnable) {		// 在主线程运行		if(android.os.Process.myTid()==BaseApplication.getMainTid()){			runnable.run();		}else{			//获取handler  			BaseApplication.getHandler().post(runnable);		}	}

修改BaseApplication

public class BaseApplication extends Application {	private static BaseApplication application;	private static int mainTid;	private static Handler handler;	@Override//  在主线程运行的	public void onCreate() {		super.onCreate();		application=this;		mainTid = android.os.Process.myTid();		handler=new Handler();	}	public static Context getApplication() {		return application;	}	public static int getMainTid() {		return mainTid;	}	public static Handler getHandler() {		return handler;	}		}

BaseFragment

public abstract class BaseFragment extends Fragment {	private LoadingPage loadingPage;	protected BitmapUtils bitmapUtils;	@Override	public View onCreateView(LayoutInflater inflater, ViewGroup container,			Bundle savedInstanceState) {		bitmapUtils = BitmapHelper.getBitmapUtils();		if (loadingPage == null) { // 之前的frameLayout 已经记录了一个爹了 爹是之前的ViewPager			loadingPage = new LoadingPage(getActivity()) {				@Override				public View createSuccessView() {					return BaseFragment.this.createSuccessView();				}				@Override				protected LoadResult load() {					return BaseFragment.this.load();				}			};		} else {			ViewUtils.removeParent(loadingPage);// 移除frameLayout之前的爹		}		return loadingPage; // 拿到当前viewPager 添加这个framelayout	}	/***	 * 创建成功的界面	 * 	 * @return	 */	public abstract View createSuccessView();	/**	 * 请求服务器	 * 	 * @return	 */	protected abstract LoadResult load();	public void show() {		if (loadingPage != null) {			loadingPage.show();		}	}	/** 校验数据 */	public LoadResult checkData(List datas) {		if (datas == null) {			return LoadResult.error;// 请求服务器失败		} else {			if (datas.size() == 0) {				return LoadResult.empty;			} else {				return LoadResult.success;			}		}	}}

这样子类只需要实现createSuccessView和load这俩个方法就可以了

自定义个xutils功能一样的BitmapHelper

public class BitmapHelper {	private BitmapHelper() {	}	private static BitmapUtils bitmapUtils;	/**	 * BitmapUtils不是单例的 根据需要重载多个获取实例的方法	 * 	 * @param appContext	 *            application context	 * @return	 */	public static BitmapUtils getBitmapUtils() {		if (bitmapUtils == null) {			// 第二个参数 缓存图片的路径 // 加载图片 最多消耗多少比例的内存(16M) 0.05-0.8f			bitmapUtils = new BitmapUtils(UiUtils.getContext(), FileUtils					.getIconDir().getAbsolutePath(), 0.3f);		}		return bitmapUtils;	}}

UiUtils 添加这俩个方法,省略了代码的书写 

	public static View inflate(int id) {		return View.inflate(getContext(), id, null);	}	public static Drawable getDrawalbe(int id) {		return getResource().getDrawable(id);	}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-11-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档