首页 > 代码库 > 抽取的BaseFragment和LoadingPage

抽取的BaseFragment和LoadingPage

【BaseFragment】:

public abstract class BaseFragment extends Fragment {
/*★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★*/
private LoadingPage loadingPage;
private ViewGroup container;
public boolean isInViewPager=false;//继承的Fragment是否存在于ViewPager中
//默认为false,如果fragment不是在viewpager中它的setUserVisibleHint方法不会执行,默认不可见
/*★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★*/
private int isLoad=0;//是否已经加载过
private boolean isVisable;//是否可见
/*★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★*/
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
this.container=container;
loadingPage=updateLoadingPage();
return loadingPage;
}

/*★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★*/
public LoadingPage updateLoadingPage(){
loadingPage=new LoadingPage(container.getContext()) {
@Override
public int getMyLayoutId() {
return getLayoutId();
}

@Override
public RequestParams getMyParams() {
return getParams();
}

@Override
public String getMyUrl() {
//如果Fragment不在ViewPager里,直接返回url
if(!isInViewPager){
return getUrl();
}
//如果Fragment在ViewPager里,需要分情况考虑
if(TextUtils.isEmpty(getUrl())){//本身就不需要联网
isLoad=1;
return getUrl();
}else{
if(isVisable){//需要联网,可见
isLoad=2;
return getUrl();
}else{//需要联网,不可见
isLoad=3;
return null;
}
}

}

@Override
public void onMySuccess(ReSultState reSultState, View successView) {
ButterKnife.bind(BaseFragment.this, successView);//绑定操作,第1个参数注意不能为this
initTitle();
initData(reSultState.getContent());
}

};
return loadingPage;

}
/*★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★*/

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
showLodingPage();
//为什么不直接写loadingPage.show()而要用方法showLodingPage()封装这句代码?
//因为showLodingPage()方法,可以使得BaseFragment的继承者都可以调用
//homeFragment=new HomeFragment();不会调用BaseFragment的onCreateView方法
//在commit()之后,Fragment的生命周期onCreateView才会执行
}

/*★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★*/
public void showLodingPage(){
loadingPage.show();//联网操作——>改变联网状态——>替换掉正在加载的页面

//模拟联网耗时操作,正常情况下用上面这句代码即可
// UIUtils.getHandler().postDelayed(new Runnable() {
// @Override
// public void run() {
// loadingPage.show();
// }
// },2000);

//还可以在LoadingPage的show()方法模拟联网耗时操作

}
/*★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★*/
@Override
public void onDestroyView() {
super.onDestroyView();
ButterKnife.unbind(this);
}
/*★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★*/
public abstract RequestParams getParams();
public abstract String getUrl();
public abstract int getLayoutId();
public abstract void initTitle();
public abstract void initData(String content);
/*★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★*/
//处理ViewPager的每页是继承于BaseFragment的Fragment,预加载的情况
//最终实现的目标:取消ViewPager的预加载+缓存所有加载过的
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
isVisable=isVisibleToUser;
if(isInViewPager && isVisibleToUser && isLoad==3){
loadingPage.show();//这时候loadingPage一定不为null,因为isLoad==3表示已经初始化过
}
}
/*★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★*/
}

【LoadingPage】:

public abstract class LoadingPage extends FrameLayout {
/*★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★*/
private Context mContext;

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_SUCESS=4;

public int state_current=STATE_LOADING;

private View loadingView;
private View errorView;
private View emptyView;
private View successView;

private LayoutParams params;

/*★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★*/
public LoadingPage(Context context) {
this(context, null);
}

public LoadingPage(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public LoadingPage(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.mContext=context;
init();
}
/*★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★*/
private void init() {
params=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
if(loadingView==null){
loadingView= UIUtils.getXmlView(R.layout.page_loading);
addView(loadingView,params);
}
if(errorView==null){
errorView= UIUtils.getXmlView(R.layout.page_error);
addView(errorView);
}
if(emptyView==null){
emptyView= UIUtils.getXmlView(R.layout.page_empty);
addView(emptyView);
}
showSafePage();
}

/**
* 保证View的显示在主线程中执行
*/
private void showSafePage() {
UIUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
showPage();
}
});
}

private void showPage() {
loadingView.setVisibility(state_current==STATE_LOADING?VISIBLE:GONE);
errorView.setVisibility(state_current==STATE_ERROR?VISIBLE:GONE);
emptyView.setVisibility(state_current==STATE_EMPTY?VISIBLE:GONE);
if(successView==null){
successView=View.inflate(mContext,getMyLayoutId(),null);//Context:使用Fragment依赖的Activity实例
addView(successView,params);
}
successView.setVisibility(state_current==STATE_SUCESS?VISIBLE:GONE);
}
/*★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★*/
private AsyncHttpClient client=new AsyncHttpClient();//联网工具
private ReSultState reSultState;//封装联网状态和数据的对象
public void show(){
//处理有些Fragment本身就不需要联网请求数据的情况,即该Fragment重写的getUrl方法直接return null
String url=getMyUrl();
if(TextUtils.isEmpty(url)){
state_current=STATE_SUCESS;
reSultState=ReSultState.SUCCESS;
reSultState.setContent("");
//这里将content设为"",那么在比如HomeFragment的initData一定要判断content是否为空
loadPage();
return;
}
//处理有些Fragment需要联网请求数据的情况,这时候该Fragment的getUrl()返回的是一个url
Runnable runnable=new Runnable() {
@Override
public void run() {
/*-------------------------------------------------------------------*/
//正常情况下的代码
AsyncHttpResponseHandler responseHandler=new AsyncHttpResponseHandler(){
@Override
public void onSuccess(String content) {//联网请求数据成功
//content="";//模拟请求数据为空
if(TextUtils.isEmpty(content)){
reSultState=ReSultState.EMPTY;
reSultState.setContent("");
}else{
reSultState=ReSultState.SUCCESS;
reSultState.setContent(content);
}
loadPage();
}
@Override
public void onFailure(Throwable error, String content) {//联网请求数据失败
reSultState=ReSultState.ERROR;
reSultState.setContent("");
loadPage();
}
};
client.get(getMyUrl(),getMyParams(),responseHandler);//getMyUrl(),getMyParams()由BaseFragment实现
/*-------------------------------------------------------------------*/
}
};
UIUtils.getHandler().post(runnable);
//UIUtils.getHandler().postDelayed(runnable, 2000);//模拟延时2秒,正常代码没有这个
}

/**
* 根据resultState的状态和数据,更新当前加载状态state_current,如果是联网请求成功还需要
* 调用抽象方法onSuccess(resultState,content),在BaseFragment会实现这个抽象方法,
* 从而根据联网成功的数据加载页面
*/
private void loadPage() {
switch (reSultState) {
case SUCCESS:
state_current=STATE_SUCESS;
break;
case ERROR:
state_current=STATE_ERROR;
break;
case EMPTY:
state_current=STATE_EMPTY;
break;
}

showSafePage();

if(state_current==STATE_SUCESS){
onMySuccess(reSultState,successView);
}
}

/*★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★*/
/**
* 封装联网状态和请求结果的枚举类
*/
public enum ReSultState{
ERROR(2),EMPTY(3),SUCCESS(4);
private int state;
private String content;
ReSultState(int state) {
this.state=state;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
/*★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★*/
// 抽象方法
public abstract int getMyLayoutId();
public abstract RequestParams getMyParams();
public abstract String getMyUrl();
public abstract void onMySuccess(ReSultState reSultState,View successView);
/*★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★*/

}

抽取的BaseFragment和LoadingPage