import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import com.heima.googleplay.R;
import com.heima.googleplay.manager.ThreadManager;
import com.heima.googleplay.utils.UIUtils; import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
/**
*
*
* ============================================================
**/
public abstract class LoadingPage extends FrameLayout {
//默认状态
private final int UN_LOADING = 1;
//加载状态
private final int LOADING = 2;
//加载失败状态
private final int ERROR = 3;
//加载成功。然后服务器没有返回数据
private final int EMPTY = 4;
//加载成功的状态
private final int SUCCESS = 5;
//用来记录某种状态
private int mState;
private View mLoadingView;
private View mErrorView;
private View mEmptyView; private View mSuccessView;
public LoadingPage(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
} private void init() {
//首先赋值
mState = UN_LOADING; mLoadingView = createLoadingView(); if(null != mLoadingView){
addView(mLoadingView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
} mErrorView = createErrorView(); if(null != mErrorView){
addView(mErrorView,new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
} mEmptyView = createEmptyView(); if(null != mEmptyView ){
addView(mEmptyView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
//成功页面根据业务不同页面将不同,所以在此无法添加统一界面
//只能在主线程中展示出来
showSafePage(); }
private void showSafePage() {
UIUtils.runInMainThread(new Runnable() { @Override
public void run() {
showPage(); }
}); }
//三种页面已经添加到FrameLayout中,通过显示和隐藏来控制显示那个
protected void showPage() {
if(null != mLoadingView){
mLoadingView.setVisibility(mState == UN_LOADING || mState == LOADING ? View.VISIBLE : View.INVISIBLE);
} if(null != mErrorView){
mErrorView.setVisibility(mState == ERROR ? View.VISIBLE : View.INVISIBLE);
} if(null != mEmptyView){
mEmptyView.setVisibility(mState == EMPTY ? View.VISIBLE : View.INVISIBLE);
} if(null == mSuccessView && mState == SUCCESS ){
mSuccessView = createSuccessView();
addView(mSuccessView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
if(null != mSuccessView){
mSuccessView.setVisibility(mState == SUCCESS ? View.VISIBLE : View.INVISIBLE);
}
} public abstract View createSuccessView();//创建成功后显示的业务界面 private View createEmptyView() {
// TODO Auto-generated method stub
return UIUtils.inflate(R.layout.loading_page_empty);
} private View createErrorView() {
// TODO Auto-generated method stub
return UIUtils.inflate(R.layout.loading_page_error);
} private View createLoadingView() {
// TODO Auto-generated method stub
return UIUtils.inflate(R.layout.loading_page_loading);
} public LoadingPage(Context context, AttributeSet attrs) {
super(context, attrs);
init();
} public LoadingPage(Context context) {
super(context);
init();
}
// //加载失败状态
// private final int ERROR = 3;
// //加载成功。然后服务器没有返回数据
// private final int EMPTY = 4;
// //加载成功的状态
// private final int SUCCESS = 5;
public enum LoadResult{
ERROR(3),EMPTY(4),SUCCESS(5);
int value ;
LoadResult(int value){
this.value = value;
}
public int getValue() {
return value;
}
}
//利用线程池
private class LoadTask implements Runnable{ @Override
public void run() {
final LoadResult result = Load();
UIUtils.runInMainThread(new Runnable() { @Override
public void run() { mState = result.getValue(); showPage();
}
}); } } public void show() {
if(mState == ERROR || mState == EMPTY){
mState = UN_LOADING;
} if(mState == UN_LOADING){
mState = LOADING; LoadTask task = new LoadTask();
ThreadManager.getLongPool().execute(task);
// ExecutorService service = Executors.newFixedThreadPool(3);
// LoadTask task = new LoadTask();
// service.execute(task);
}
showSafePage();//加载完数据,在主线程中展示界面出来
}
//从服务器加载数据,返回加载状态(加载数据成功、失败?)
public abstract LoadResult Load() ; }
import java.util.List; import com.heima.googleplay.utils.UIUtils;
import com.heima.googleplay.utils.ViewUtils;
import com.heima.googleplay.widget.LoadingPage;
import com.heima.googleplay.widget.LoadingPage.LoadResult; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent; public abstract class BaseFragment extends Fragment { private LoadingPage mContentPage; //setContentView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//当前的view已经有了一个父亲。必须移除
if(mContentPage == null){
mContentPage = new LoadingPage(UIUtils.getContext()) { @Override
public View createSuccessView() {
// TODO Auto-generated method stub
return BaseFragment.this.createSuccessView();
} @Override
public LoadResult Load() {
// TODO Auto-generated method stub
return BaseFragment.this.load();
}
};
}else{
ViewUtils.removeSelfFromParent(mContentPage);
} return mContentPage;
}
/**
* 检查服务器返回的json数据
* @param object
* @return
*/
protected LoadResult chece(Object object) {
if (object == null) {
return LoadResult.ERROR;
}
if (object instanceof List) {
List list = (List) object;
if (list.size() == 0) {
return LoadResult.EMPTY;
}
} return LoadResult.SUCCESS;
} protected abstract LoadResult load(); protected abstract View createSuccessView(); //在MainActivity中调用
public void show() {
if (null != mContentPage) {
mContentPage.show();
} } }
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu; import com.heima.googleplay.fragment.AppFragment;
import com.heima.googleplay.fragment.BaseFragment;
import com.heima.googleplay.fragment.FragmentFactory;
import com.heima.googleplay.fragment.GameFragment;
import com.heima.googleplay.fragment.HomeFragment;
import com.heima.googleplay.utils.UIUtils;
import com.heima.googleplay.widget.PagerTab; public class MainActivity extends BaseActivity implements OnPageChangeListener { private PagerTab mTabs;
private ViewPager mPager; @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} private class MainAdapter extends FragmentStatePagerAdapter{ private String[] tab_names;
private BaseFragment fragment; public MainAdapter(FragmentManager fm) {
super(fm);
tab_names = UIUtils.getStringArray(R.array.tab_names);
} @Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return tab_names[position];
} @Override
public Fragment getItem(int position) { return FragmentFactory.createFragment(position);
} @Override
public int getCount() {
// TODO Auto-generated method stub
return tab_names.length;
} } @Override
protected void initView() {
setContentView(R.layout.activity_main);
mTabs = (PagerTab) findViewById(R.id.tabs);
mPager = (ViewPager) findViewById(R.id.pager);
MainAdapter adapter = new MainAdapter(getSupportFragmentManager());
mPager.setAdapter(adapter);
//绑定到一起
mTabs.setViewPager(mPager);
mTabs.setOnPageChangeListener(this);
} @Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub } @Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub } @Override
public void onPageSelected(int arg0) {
BaseFragment fragment = FragmentFactory.createFragment(arg0);
fragment.show();
} }
MainActivity