5.代码抽取(加载界面)
简单框架的搭建主要就是泛型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);
}