分享一个Andriod Base类

来源:互联网 发布:windows正版价格 编辑:程序博客网 时间:2024/05/21 01:47

出于一些需要,自己整理了一个base类方便开发。先说明一下,这里没有多高深的原理或者技术,只是单纯的分享一点经验。给大家提供一点思路。

需求

在加载的时候显示LoadingView,在加载错误的时候显示ErrorView,加载成功时则显示正常的内容ContentView。

布局

BaseActivity的布局文件act_base.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:clipToPadding="true"    android:fitsSystemWindows="true">    <FrameLayout        android:id="@+id/fl_base_header"        android:layout_width="match_parent"        android:layout_height="wrap_content"/>    <FrameLayout        android:id="@+id/fl_base_container"        android:layout_below="@id/fl_base_header"        android:layout_width="match_parent"        android:layout_height="match_parent"/>    <FrameLayout        android:id="@+id/fl_base_loading"        android:layout_below="@id/fl_base_header"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:visibility="gone"/>    <FrameLayout        android:id="@+id/fl_base_error"        android:layout_below="@id/fl_base_header"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:visibility="gone"/></RelativeLayout>   

可以 看到我这里写了四个FrameLayout,其中fl_base_header是预留给顶部标题栏使用,fl_base_container预留给内容填空使用,fl_base_loading预留给加载中页面使用,fl_base_error预留给加载错误时使用。

开始添加

下面我们把这些View合理添加到BaseActivity类里去。

    FrameLayout headerLayout;    FrameLayout containerLayout;    FrameLayout loadingView;    FrameLayout errorView;      private void findViews(){        headerLayout= (FrameLayout) findViewById(R.id.fl_base_header);        containerLayout= (FrameLayout) findViewById(R.id.fl_base_container);        loadingView= (FrameLayout) findViewById(R.id.fl_base_loading);        errorView= (FrameLayout) findViewById(R.id.fl_base_error);    }    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        initBaseData();        super.setContentView(R.layout.act_base);        findViews();        initHeaderLayout();        initLoadingView();        initErrorView();    }

重写setContentView()

在onCreate()方法中,先调用父类的setContentView方法,将act_base加载。然后,我们需要重写setContentView方法,方便子类调用设置

 @Override    public void setContentView(@LayoutRes int layoutResID) {        View view= LayoutInflater.from(this).inflate(layoutResID,containerLayout,true);        ButterKnife.bind(this,view);        initActivity();    }    @Override    public void setContentView(View view) {        containerLayout.addView(view);        ButterKnife.bind(this,view);        initActivity();    }    @Override    public void setContentView(View view, ViewGroup.LayoutParams params) {        containerLayout.addView(view,params);        ButterKnife.bind(this,view);        initActivity();    }

在上面我将setContentView里的参数view添加到了containerLayout上,也就是要显示的内容里面。

添加HeaderView

  /**     * 添加头部布局,类似自定义toolbar     * @param headerLayoutResID     */    public void setHeaderLayout(@LayoutRes int headerLayoutResID){        View headerView= LayoutInflater.from(this).inflate(headerLayoutResID,headerLayout,true);    }    public void setHeaderLayout(View headerView){        headerLayout.addView(headerView);    }

添加LoadingView

 /**     * 添加loadingView     * @param layoutResID     */    public void setLoadingView(@LayoutRes int layoutResID){        LayoutInflater.from(this).inflate(layoutResID,loadingView,true);    }    public void setLoadingView(View loadingView){        this.loadingView.addView(loadingView);    }

添加ErrorView

 /**     * 添加错误显示布局     * @param layoutResID 布局ID     */    public void setErrorView(@LayoutRes int layoutResID){        LayoutInflater.from(this).inflate(layoutResID,errorView,true);    }    /**     * 添加错误显示布局     * @param errorView 错误View     */    public void setErrorView(View errorView){        this.errorView.addView(errorView);    }

通过状态量控件显示哪个View

/**     * 通过状态显示加载页面,内容页面,错误页面     * @param showLoading     * @param showContainer     * @param showErrorView     */    public void showViewByState(boolean showLoading,boolean showContainer,boolean showErrorView){        showView(loadingView,showLoading);        showView(containerLayout,showContainer);        showView(errorView,showErrorView);    }    private void showView(View view, boolean show){        if(show){            view.setVisibility(View.VISIBLE);        }else{            view.setVisibility(View.GONE);        }    }

写一个风格合适的头、加载、错误页面

一般来说,每个app的加载显示,错误页面显示都是一样的,所以为了更加方便,我们再写一个默认的标题栏,加载页面与错误显示页面。
不想写了,直接上demo。下载地址:Android Base类

0 0