博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android 应用架构随笔六(Loading加载页面)
阅读量:6118 次
发布时间:2019-06-21

本文共 9552 字,大约阅读时间需要 31 分钟。

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();        }    }}
View Code
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

 

转载于:https://www.cnblogs.com/ecollab/p/6079983.html

你可能感兴趣的文章
直播视频流技术名词
查看>>
网易跟贴这么火,背后的某个力量不可忽视
查看>>
企业级java springboot b2bc商城系统开源源码二次开发-hystrix参数详解(八)
查看>>
java B2B2C 多租户电子商城系统- 整合企业架构的技术点
查看>>
IOC —— AOP
查看>>
比特币现金将出新招,推动比特币现金使用
查看>>
数据库的这些性能优化,你做了吗?
查看>>
某大型网站迁移总结(完结)
查看>>
mysql的innodb中事务日志(redo log)ib_logfile
查看>>
部署SSL证书后,网页内容造成页面错误提示的处理办法
查看>>
MS SQLSERVER通用存储过程分页
查看>>
60.使用Azure AI 自定义视觉服务实现物品识别Demo
查看>>
Oracle 冷备份
查看>>
jq漂亮实用的select,select选中后,显示对应内容
查看>>
C 函数sscanf()的用法
查看>>
python模块之hashlib: md5和sha算法
查看>>
linux系统安装的引导镜像制作流程分享
查看>>
解决ros建***能登录不能访问内网远程桌面的问题
查看>>
pfsense锁住自己
查看>>
vsftpd 相关总结
查看>>